简体   繁体   中英

Can't append li into ul with Jquery

Been going through these 3 lines for the past 15 minutes, but i just don't see it... EDITED CLASS NAMES. html

<div class="parent" >
<ul class="child" style="height:auto !important"></ul>
</div>

jquery

$(document).ready(function(){
$(".parent ul").append('<li>wtf</li>');
});

I've tried appending to .child, to .parent ul.child no luck

Edit: when i look at the fiddle in the answer bellow, it works. I copy the same code and it fails. I tried all browsers. same thing.

Your code:

$(document).ready(function(){
    $(".1 ul").append('<li>wtf</li>');
});

Works fine.

Demo

Problem is not in code, you added to your question. Are you sure you included jquery library?

Please, use firebug tool or any other tool to debug your code (chrome developers tools (CTRL+SHIFT+J), developers tools in ie (possibly F12), developers tools in Firefox (CTRL+SHIFT+J). You can find some good answers here (read answers).

In additional:

As mentioned in comments to your question (and at w3cschools website):

Do NOT start a class name with a number!

So use this:

jQuery:

$(document).ready(function(){
    $(".parent ul").append('<li>wtf</li>');
});

HTML:

<div class="parent" >
<ul class="child" style="height:auto !important"></ul>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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