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();
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();
This selector matches any tag that is:
<body>
print-modal
and<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:
:not()
selectorIf 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.
>
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.
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.
$('body > *:not(#print-modal):not(script)').clone();
意味着,克隆 body 的所有直接子元素,但不克隆任何具有 idprint-modal
而不是script
标签的元素。
也可以写成
$('body > *').not('#print-modal, script').clone();
参考: jQuery。