2017-12-29 13 views
-2

ul li를 사용하여 목록을 구현하고 있습니다. 따라서 이미 4 개의 목록 값이 있고 모든 값은 null이지만 하나의 변수가 일부 값을 가져 와서 기존 값으로 대체하려고합니다. 내 코드를 공유하고 있습니다.자바 스크립트에서 목록을 업데이트하는 방법?

스크립트

var emptyLegend='<ul> 
<li>red(0)</li> 
<li>yellow(0)</li> 
<li>green(0)</li> 
<li>purple(0)</li> 
</ul>' 

var legend='<ul> 
<li>red(3)</li> 
</ul>' 

변수 범례는 동적이다. VAR 전설이있는 경우 빨간색 (4), 노란색 (5)를 출력해야한다 :

'<ul> 
    <li>red(4)</li> 
    <li>yellow(5)</li> 
    <li>green(0)</li> 
    <li>purple(0)</li> 
    </ul>' 

내가 빨간색 교체하려면 (0 개)와 (3);

답변

0

이 같은 시도 할 수 있습니다 :

<!DOCTYPE html> 
<html> 
<body> 

<ul id="myList"><li>red(0)</li> 
<li>yellow(0)</li> 
<li>green(0)</li> 
<li>purple(0)</li></ul> 

<p>Click the button to replace the first item in the the list.</p> 

<button onclick="myFunction()">Try it</button> 

<script> 
function myFunction() { 
    var textnode = document.createTextNode("red(3)"); 
    var item = document.getElementById("myList").childNodes[0]; 
    item.replaceChild(textnode, item.childNodes[0]); 
} 
</script> 

</body> 
</html> 

이것은 당신의 논리를 추가 할 생각입니다. More Info

+0

리로도 바꾸고 싶습니다. –

+0

왜 리 –