English 中文(简体)
Using jQuery NOT selector to exclude elements with particular children.
原标题:

I have a function that appends some HTML to an element. It may get called more than once, so I only want it to append the HTML to elements that haven t already had the HTML added.

Example:

<div class="divToAppendTo"></div>
<div class="divToAppendTo"><span class="myAppendedMarkup"></span></div>

As such, I d like to select all .divToAppendTo that don t have an immediate child of .myAppendedMarkup

My stab at it doesn t seem to work:

$(".divToAppendTo:not(>span.myAppendedMarkup)")

It always appends when I call it (thereby duplicating the content).

最佳回答

try

$("div.divToAppendTo:not(:has(span.myAppendedMarkup))")

or

$("div.divToAppendTo").filter(function() { 
    return $(this).children( span.myAppendedMarkup ).length == 0;  
});
问题回答

If you don t mind, I think I may have an easier solution for what you re trying to do.

Whenever you append HTML to a .divToAppendTo do a $(this).removeClass("divToAppendTo"); this way you ll just need to select .divToAppendTo while being sure that it doesn t have any code appended to it.

If you are using this class for something else, you can always use another one to do this.

can you do:

$(".divToAppendTo:not(:has(span.myAppendedMarkup))");

I like this for its readability, and it s comparable in efficiency to the fastest suggestion in other answers.

var completeSet = $( "div.divToAppendTo" );
var unwantedSet = completeSet.has( "span.myAppendedMarkup" );    
var wantedSet   = completeSet.not(unwantedSet);

If you don t need to leave the className in place go for Soufiane s answer, otherwise this should work:

$( div.divToAppendTo ).filter(function() {
    return $(this).html() ==   ;
}) );

Or if you expect some other HTML in the .divToAppendTo s, then use this:

$("div.divToAppendTo").filter(function() { 
    return $(this).children( span.myAppendedMarkup ).length == 0;  
});




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签