GetElementByTagName[ 0 ].setAttribute inside a loop
GetElementByTagName[ 0 ].setAttribute inside a loop
我只是对此感到好奇。
我创建了一个将 ID 值添加到动态创建 li 标签的函数。
该函数如下所示。
function limenu(lix,liy){
document.getElementsByTagName("li")[0].setAttribute("id",lix);
document.getElementsByTagName("li")[1].setAttribute("id",liy);
} limenu("icon-dice","icon-clock");
我的想法是,我可以使用for循环吗?这让我可以插入我想要的事件数量,而无需创建document.getElementsByTagName("li")[1]-[2]-[3]
..等
因此,当我调用函数 liemenu() 时,我将能够添加任意数量的事件。
我可以使用Jquery,但我真的很想用Javascript来做这件事。
谢谢
function limenu() {
var lis = document.getElementsByTagName("li");
var count = Math.min(lis.length, arguments.length);
for (var x = 0; x < count; x++) {
lis[x].setAttribute('id', arguments[x]);
}
}
http://jsfiddle.net/ExplosionPIlls/72hdS/
相关文章:
- 对于loop.if-仅在经过所有间隔后返回true
- jQuery setTimeOut: in for-loop
- $(window).height() inside iframe?
- php inside javascript
- javascript/angularjs 将 String 与 number for loop 连接起来
- Loop什么都没做
- How to target an h1 inside an <a>?
- JSON:loop Invoice Items:如果InvoiceNo未退出则添加,如果退出则更新值,示例包括在内
- javascript inside laravel 5
- JavaScript for loop
- Coffeescript jQuery each loop
- jQuery $(this) inside function
- jquery .change inside loop
- for loop inside another for loop javascript (jquery)
- setTimeout is not working correctly inside "for loop&qu
- Form Inside li and inside Loop
- jQuery replaceWith Inside Loop malfunction
- javascript/google map api if statement inside for loop
- GetElementByTagName[ 0 ].setAttribute inside a loop
- jQuery and setTimeout inside For loop