문제

I have an unordered list (which can nest) such as:

<div id="checklist">
    <ul>
        <li>On Page SEO
            <ul>
                <li>My work <i>And note.</i>
                <li>Your work <i>Blah</i>
            </ul>
        </li>
        <li>Is this love?</li>
    </ul>
</div>

On document ready, I want to add a checkbox to each <li> tag BEFORE the text. I currently have the following which adds it AFTER the text:

$(document).ready(function () {
     $('<input type="checkbox" value="1" />').appendTo('li');
 });
도움이 되었습니까?

해결책

using prependTo() instead of appendTo() will do the trick

다른 팁

try this:

$(document).ready(function () {
     $('div#checklist ul li').prepend('<input type="checkbox" value="1" />');
 });

Or:

$(document).ready(function () {
    $('<input type="checkbox" value="1" />').prependTo("li");
 });

I think what you're trying to do is to append checkbox next to any li that does not contain a ul inside it, if so then you can do:

$('ul li').not(':has("ul")').append('<input type="checkbox" value="1" />');

You also need to close the <li> using </li> in your HTML markup.

Fiddle Demo

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top