0

所以我觉得我拥有的代码应该可以工作,但事实并非如此。目的是从除特定元素之外的每个元素中删除可编辑的类。

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

我也试过做

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

我确信我在某个地方犯了一些愚蠢的错误,但请帮助我。

编辑:很抱歉没有指定人。gCms.section 是一个字符串,包含类似于“#someID”的内容

4

4 回答 4

2

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.

于 2013-10-24T16:18:48.140 回答
0

我想你的意思是$('*:not(#gCms.section)').removeClass("cmsEdit")

并且您gCms.section需要一个标识符,例如idor class(pound == id, period == class)。

于 2013-10-24T16:12:29.610 回答
0

你确定你不是不小心把它们扭曲了,你真的是这个意思吗?

$("*").not("section.gCms").removeClass("cmsEdit");
于 2013-10-24T16:15:40.083 回答
0

我认为你可以这样做:

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

它从所有元素中替换 cmsEdit,但不是从 Id gCms => 类部分中替换。

于 2013-10-24T16:17:31.687 回答