简体   繁体   English

使用appendChild将li附加到ul

[英]Appending li to ul using appendChild

hi i have a images and i want to create a nav li for each image item.. Now i have this code 您好,我有一个图片,我想为每个图片项目创建一个导航。.现在我有了这段代码

 var bigImages = $('#imagesList li img'); // gets the Big Images

Then i have created a ul with this id 然后我用这个ID创建了一个UL

<ul id="thumbsList">

    </ul>

Now using a loop i want to add a blank <li> </li> as equal to the big images in the array.. 现在使用循环,我想添加一个空白<li> </li>等于数组中的大图像。

var rounds = document.getElementById('thumbsList');



for (var i = 0; i < bigImages.length; i++) {
        rounds.appendChild('<li> /li>');
    }

Now the console displays this error 现在控制台显示此错误

`Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node' `Uncaught TypeError:无法在'Node'上执行'appendChild':参数1不是'Node'类型的

Please tell me where am i doing it wrong.thanks`. 请告诉我我在哪里做错了。谢谢。

You are doing it wrong. 你做错了。 Since you are using plain javascript, I will answer this in same: 由于您使用的是纯JavaScript,因此我将以相同的方式回答:

for (var i = 0; i < bigImages.length; i++) {
    var p = document.createElement("li"); //This is what you want
    rounds.appendChild(p);
}

Demo: http://jsfiddle.net/GCu2D/909/ 演示: http//jsfiddle.net/GCu2D/909/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM