开发者

jQuery if visible

开发者 https://www.devze.com 2023-03-29 11:55 出处:网络
<div id=\"chatCenterMembers\"> <div class=\"chatmember\"> <a title=\"Blah Blah Blah\">
 <div id="chatCenterMembers">
   <div class="chatmember">
       <a title="Blah Blah Blah">
          <div class="newchatmessage" style="display: block;"></div>

How can I capture the visible div in an if statement?

I have $(this) set to <div class="chatmember"> - second line from the top.

I've been working with below but had now luck so far.

if($(this开发者_开发技巧+' a div.newchatmessage').filter(":visible")) {

Above just drops out...

I've also tried below and it doesn't work either

if ($(this + 'a div.newchatmessage').is(':visible')) {


Use .is() to check if an element fills a certain requirement, like such:

if ($(this).find('a div.newchatmessage').is(':visible'))

Or, if you want it more readable:

var element = $(this).find('a div.newchatmessage');

if (element.is(':visible')) {
    // Do your thing
}


.is()

for any detailed information on this method, just scroll down a bit ... there's a bunch of examples!

edit (thanks for the hint @Wesley Murch): if this does not work, your selector might be wrong ... $(this+' a div.newchatmessage') looks quite strange ... it might rather be $('a div.newchatmessage', this) or $('a div.newchatmessage', $(this)) depending on this being a jQuery-variable or not


I ran into a similar situation and wanted to note that you should be wary of the jquery selector returning more than one element. I believe the .is(':visible') check will only evaluate the first element in the array. You can check to see if all elements returned by that selector are visible with something like this.

if($('.someElementWithThisClass:visible').length > 0){
    //Do it to it
}
0

精彩评论

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