jquery 忽略子元素
您好,我有一个带有嵌套 div 的 div 标签,如下所示
<div class="friendRequestMenu" id="friendRequestMenu">
<div class="toolbarTitle">
<span>Friend Requests</span> <a href="javascript:void(0)" class="hyperlink">Find Friends</a>
</div>
<div id="friendRequestData">
<!-- put div class=requestli to see a block-->
<div class="no-request">
No New Friend Requests
</div>
<a href="javascript:void(0)">
<div class="see-all-requests">
See All Requests
</div>
</a>
</div>
</div>
javascript:
$('*').each(function() { /getting all elements in document
// do something for all elements except the div tag above
});
我希望代码适用于除 div 标签及其子项之外的所有内容,我知道我可以获取所有 id 并添加例外,但我想这样做仅包含父标签 谢谢 编辑:
嗨,这正是我想要做的
$(document.body).find('*:not(#friendRequestMenu):not(#friendRequestMenu *)').each(function () {
var clientWidth = $(window).width();
var clientHeight = $(window).height();
var heightPercent = ($(this).height() / clientHeight) * 100;
var widthPercent = ($(this).width() / clientWidth) * 100;
$(this).css('height', heightPercent.toString());
$(this).css('width', widthPercent.toString());
});
,但我想忽略一些元素,因为你可以看到选择器部分代码对我不起作用
hi i have a div tag with nested divs as shown below
<div class="friendRequestMenu" id="friendRequestMenu">
<div class="toolbarTitle">
<span>Friend Requests</span> <a href="javascript:void(0)" class="hyperlink">Find Friends</a>
</div>
<div id="friendRequestData">
<!-- put div class=requestli to see a block-->
<div class="no-request">
No New Friend Requests
</div>
<a href="javascript:void(0)">
<div class="see-all-requests">
See All Requests
</div>
</a>
</div>
</div>
javascript:
$('*').each(function() { /getting all elements in document
// do something for all elements except the div tag above
});
i want the code to apply for everything except for the div tag and its children, i know i can get all the id's and add an exception , but i want to do that with only the parent tag
thanks
EDIT:
hi this is exactly what i want to do
$(document.body).find('*:not(#friendRequestMenu):not(#friendRequestMenu *)').each(function () {
var clientWidth = $(window).width();
var clientHeight = $(window).height();
var heightPercent = ($(this).height() / clientHeight) * 100;
var widthPercent = ($(this).width() / clientWidth) * 100;
$(this).css('height', heightPercent.toString());
$(this).css('width', widthPercent.toString());
});
but i want to ignore some elements, as you can see the selector part code isnt working for me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
:not()
选择器应该可以很好地处理这个问题。Using the
:not()
selector should handle this nicely.最简单的形式如下:
第二
个版本可能更有效,因为我不相信浏览器的
querySelectorAll
的本机实现支持:not(...).除非您对查询需要多长时间有疑问,否则不必费心优化。
The simplest forms are as follows:
and
The second version is likely to be more efficient as I don't believe that the browser's native implementation of
querySelectorAll
supports:not(...)
. Don't bother optimizing unless you have an issue with how long the query takes.试试这个
Try this