最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery ignore child elements - Stack Overflow

programmeradmin2浏览0评论

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

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

Share Improve this question edited Jul 20, 2011 at 5:21 Yohann asked Jul 19, 2011 at 14:46 YohannYohann 1362 silver badges13 bronze badges 2
  • If you take out the div tags and their children, then there's nothing left. – Oswald Commented Jul 19, 2011 at 14:49
  • 2 I would suggest you reconsider if using the * selector is a good idea. Maybe what you want to do is possible without iterating over the zillion elements that make up a production web page? – Jon Commented Jul 19, 2011 at 14:49
Add a ment  | 

3 Answers 3

Reset to default 5
$('*:not(#friendRequestMenu):not(#friendRequestMenu *)').each(...);

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

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.

Try this

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

评论列表(0)

  1. 暂无评论