jquery 忽略子元素

发布于 2024-11-24 16:18:30 字数 1451 浏览 1 评论 0原文

您好,我有一个带有嵌套 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

潜移默化 2024-12-01 16:18:30
$('*:not(#friendRequestMenu):not(#friendRequestMenu *)').each(...);

使用 :not() 选择器应该可以很好地处理这个问题。

$('*:not(#friendRequestMenu):not(#friendRequestMenu *)').each(...);

Using the :not() selector should handle this nicely.

栖竹 2024-12-01 16:18:30

最简单的形式如下:

$(':not(#friendRequestMenu, #friendRequestMenu *)').each(...);

第二

$('*').not('#friendRequestMenu, #friendRequestMenu *');

个版本可能更有效,因为我不相信浏览器的 querySelectorAll 的本机实现支持 :not(...).除非您对查询需要多长时间有疑问,否则不必费心优化。

The simplest forms are as follows:

$(':not(#friendRequestMenu, #friendRequestMenu *)').each(...);

and

$('*').not('#friendRequestMenu, #friendRequestMenu *');

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.

为你拒绝所有暧昧 2024-12-01 16:18:30

试试这个

$('*').not("#friendRequestMenu, #friendRequestMenu *").each(function(){
  //do something here
});

Try this

$('*').not("#friendRequestMenu, #friendRequestMenu *").each(function(){
  //do something here
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文