开发者

How to use jquery to find the element with display=none and return the elements id to a variable?

I am using jquery to to find the element on the page with display set to none开发者_开发问答 and return it's id in a variable. My attempt is below:

$(".galleryitem[display='none']").this

Can someone tell me where I am going wrong...


I don't think that you need to add :hidden psuedo selector. The following will give you the id of selector irrespective of whether it is hidden or not.

var elementId = $(".galleryitem").attr("id");

but if you add it will be bit faster-

var elementId = $(".galleryitem:hidden").attr("id");


  $(".galleryitem:hidden").attr("id");


Since jQuery 1.3.2, an element is visible if its browser-reported offsetWidth or offsetHeight is greater than 0. What does this change mean? It means that if your element’s CSS display is “none”, or any of its parent/ancestor element’s display is “none”, or if the element’s width is 0 and the element’s height is 0 then an element will be reported as hidden.

Example:

This means the .galleryitem element is recognized as hidden only if the parrent has display: none style:

var elementId = $(".parent .galleryitem:hidden").attr("id");

or

var elementId = $(".galleryitem:hidden").attr("id");

You can choose the example that works best for you.


var elementId = $(".galleryitem:hidden").attr("id");


To find hidden elements you can use the :hidden psuedo selector.

$(".galleryitem:hidden").each(function(){
   //do something with each element.
});

Or if you only have one item you can simply do the following:

var id = $(".galleryitem:hidden")[0].id

Example on jsfiddle

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜