jquery filtering has + not

前端 未结 5 458
离开以前
离开以前 2020-12-01 04:41

Okay I have list items, some have a span, some not.
On my event I want to add the span when they don\'t have any yet.

has() works fine, but

5条回答
  •  孤街浪徒
    2020-12-01 04:59

    You can use a combination of the :not and :has selectors like this

    $("a.add-span").click(function() {
        $("li:not(:has(span))").each(function(index) {
            $(this).append('new span<\/span>');
        });
    });
    

    Here is a demo http://www.jsfiddle.net/xU6fV/

提交回复
热议问题