简体   繁体   中英

remove class from all elements but 1

So i feel like the code I have should work but it doesn't. the intention is to remove the class editable from every element except a specific one.

$('*').not(gCms.section).removeClass("cmsEdit")

I have also tried doing

$('*').each().not(gCms.section).removeClass("cmsEdit")

I am sure I am making some dumb mistake somewhere but please help me out guys.

Edit: Sorry for not specifying guys. gCms.section is a string containing something along the lines of "#someID"

You should always try and avoid the $( "*" ) selector. It matches every single element on your page. That includes the <body> and <head> and...well...everything! IMO it really is overkill and I'm not too sure why it exists! I've never seen an instance where you can't avoid it.

Instead of trying to encompass this action into one command, you could always accomplish this task with some code similar to this:

// For any element containing the class cmsEdit, remove it.
$( ".cmsEdit" ).removeClass( "cmsEdit" );
// (Re)add the class to the element you want
$( other_element ).addClass( "cmsEdit" );

What you loose in "elegance" by performing a "one-liner" command, you gain in readability.

I think you mean $('*:not(#gCms.section)').removeClass("cmsEdit")

And your gCms.section needs an identifier such as an id or class (pound == id, period == class).

你确定你没有意外地扭曲它们,你真的是这个意思吗?

$("*").not("section.gCms").removeClass("cmsEdit");

you can do this like this i think:

var element =  $("#gCms.section");
$('*').each().not(element).removeClass("cmsEdit");

It replace cmsEdit from all element but not from Id gCms => class section.

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