元芳怎么了
2018-08-17 10:09:10
想實現js每次點擊,不斷增加一個節點,節點的文本節點的數字比上一個節點的文本節點的數字增加一,但是怎么出現增加為NAN,已經用parseInt將字符串轉換為數字了??赡苷Z言描述不清晰,具體的的代碼:希望大家能夠幫忙解答,謝謝|
1 回答

Qyouu
TA貢獻1786條經驗 獲得超11個贊
<body>
<ul class="oUl"><li>1</li><li>2</li><li>3</li><li>4</li></ul>
<input class="oBtn" type="button" value="按鈕">
</body>
<script>
var oBtn = document.getElementsByTagName("input")[0];
oBtn.onclick = append;
function append() {
var oUl = document.getElementsByTagName("ul")[0];
var cRlitextnode = parseInt(oUl.lastChild.innerText);
cRlitextnode++;
console.log(cRlitextnode);
var li = document.createElement("li");
var textNode = document.createTextNode(cRlitextnode);
oUl.appendChild(li);
li.appendChild(textNode);
}
</script>
li要寫成一行的,空白節點也是節點,以上是測試通過的代碼
添加回答
舉報
0/150
提交
取消