3

I was going through a p rewritten jQuery code . I'm not able to understand the following code .

$('body > *:not(#print-modal):not(script)').clone();
4

5 回答 5

8

This selector matches any tag that is:

  • A direct child of <body>
  • Does not have the ID print-modal and
  • Is not a <script> tag.

It then clones all these elements with .clone(), although nothing is done with the clone()d object, which is strange.

A more in-depth explanation:

body > * means "select all elements that are direct descendants of <body>", the wildcard * selecting every tag. Next, the two :not() pseudo-classes filter remove the element with the ID print_modal, as well as any <script> tags.

Reference:

于 2012-07-12T19:07:50.737 回答
2

If I am correct, in this case >* is a selector for all children of the body tag, with :not(#print-modal) and :not(script) providing two exclusions from the selector.

于 2012-07-12T19:07:43.167 回答
1

> means "direct chidren of".

* means "any tag".

So, this is saying find any tags (that aren't <script> or have id="print-modal") that are 1st level children of <body>.

Note: the * is not needed here as :not will assume the * if there is nothing before it.

于 2012-07-12T19:08:12.373 回答
0

It selects all elements that are direct children of the body that are not script tags and do not have the id print-modal.

* in a jQuery selector means all elements.

于 2012-07-12T19:08:02.950 回答
0
$('body > *:not(#print-modal):not(script)').clone();

意味着,克隆 body 的所有直接子元素,但不克隆任何具有 idprint-modal而不是script标签的元素。

也可以写成

$('body > *').not('#print-modal, script').clone();

参考: jQuery

于 2012-07-12T19:09:07.440 回答