Does jQuery append() close html tags?

若如初见. 提交于 2020-01-06 16:40:43

问题


The append cmd doesn't close my tags, but why?

What do i have to change?

for (var i = 0; i<=4; i++)
     $("#wrapper").append('<li id=img'+i+'></li>');

This always creates only <li id=img1>, but no </li>?!


回答1:


Your code should be:

for (var i = 0; i<=4; i++)
    $("#wrapper").append('<li id="img'+i+'"></li>');

I think you forgot the quotes for the id attribute




回答2:


The code you provided does seem to add the tags you want.

If I run the exact javascript you provided, viewing generated source using the web developer toolbar in firefox gives me <li id="img2"></li> and inspecting the element in firebug shows me <li id="img2"/>.

Both of those are closed and proper xhtml. Though if you aren't using xhtml, the closing tag isn't required anyway.



来源:https://stackoverflow.com/questions/1437870/does-jquery-append-close-html-tags

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!