开发者

Find tag <a> and put a word in an element with jQuery

What I want:

if inside tag <li> there is no tag <a>, then - insert word "Empty" into <div class="tooltip"> </div>.

How can do i do it with jQuery?

<ol>
    <li><input name="hotel_id[]" value="14" style="display:none;">
    <div class="tooltip" style="top: 439px; left: 717px; display: none; ">
    </div>
    <a href="" class="tool_tip" title="ok">Hello</a></li>开发者_如何学C

    <li><input name="hotel_id[]" value="5" style="display:none;">
    <div class="tooltip" style="opacity: 0.9; top: 439px; left: 717px; display: none; ">
    </div>
    <a href="" class="tool_tip" title="">How</a></li>

    <li><input name="hotel_id[]" value="4" style="display:none;">
    <div class="tooltip" style="top: 439px; left: 717px; display: none; opacity: 0.9; ">
    </div>
    <a href="" class="tool_tip" title="">Are</a></li>

    <li><input name="hotel_id[]" value="3" style="display:none;">
    <div class="tooltip" style="opacity: 0.9; top: 439px; left: 717px; display: none; ">
    </div>
    <a href="" class="tool_tip" title="">Help</a></li>

    <li><input name="hotel_id[]" value="2" style="display:none;">
    <div class="tooltip" style="top: 439px; left: 717px; display: none; ">
         // I want append here word "Empty" that there is not tag a in li
    </div></li>

    <li><input name="hotel_id[]" value="1" style="display:none;">
    <div class="tooltip" style="top: 439px; left: 717px; display: none; ">
         // I want appen here word "Empty" that there is not tag a in li
    </div></li>
</ol>


look after :not and :has:

$("li:not(:has(a))").find(".tooltip").html("Empty").removeAttr("style");

I remove the style attribute on .tooltip, if you dont need that just change it to show() instead to show the .tooltip:

$("li:not(:has(a))").find(".tooltip").html("Empty").show();

Demo: http://jsbin.com/uyuwoy/edit


This would work:

$('ol li').not(':has(a)').find('.tooltip').html('Empty');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜