追加與增量numereic值不同類我有一個跨度標籤和按鈕標籤Dynamiclly通過點擊事件
<span class="myspan">1</span>
<button id="add">Add +1</button>
var arr=["myspan1","myspan2","myspan3","myspan4"}
我想通過點擊按鈕添加新的類更span標籤從這個數組增量值。 像這樣的輸出:
<span class="myspan1">1</span>
<span class="myspan2">2</span>
<span class="myspan3">3</span>
<span class="myspan4">4</span>
我嘗試' this JsFiddle
但我不能對新追加的標籤從數組中添加類名。
另一個有用的鏈接,從陣列 http://jsbin.com/nojipowo/2/edit?html,css,js,output ... 新類附加標籤,但我不能在任何情況下,把我的願望輸出...在這裏輸入的代碼
值increaseesenter這裏這個代碼片斷
<script> var i = 0; function buttonClick() {i++; document.getElementById('inc').value = i; } </script> <button onclick="buttonClick();">Click Me</button> <input type="text" id="inc" value="0"></input>
另一種嘗試......任何人都可以幫助..讓慾望OU tput的
var i=6;
var backgrounds = ["myspan1", "myspan2", "myspan4"];
var elements = document.getElementsByClassName("myspan");var len = backgrounds.length;
$("#add").click(function() {
(i < elements.length){
$(".new-field").append('<span class="myspan">1</span><script');
var value = parseInt($(".myspan").text(), 10) + 1;
elements[i].className += ' ' + backgrounds[i%len];
i++;
$(".background").text(i);
}
});
*/
<span class="myspan">1</span>
<button id="add">Add +1</button>
<div class="new-field">
</div>
你那裏工作了嗎? –