javascript - .is("visible") & .is(":visible") working differently - Stack Overflow

I am using jquery visible selector to check if a child element is visible or not.But surprisingly .is(&

I am using jquery visible selector to check if a child element is visible or not.But surprisingly .is("visible") & .is(":visible") is showing different result on using it with css visibility:hidden property.When I am using .is('visible') it is alerting false & on using .is(":visible") is it alerting true. But on changing the css property to display:none, the result is consistent.Here is the code.

HTML

<div id="parentEle">
    I have hidden span
    <span class="hiddenContent">
        I am hiddenContent
    </span>
</div>
<button type="button" onclick="_checkChild()">Check Child</button>

JS

function _checkChild(){
    var x= false;
    x =$("#parentEle").children(".hiddenContent").is(":visible");
    alert(x);
}

CSS

.hiddenContent{
   visibility:hidden
}

jsfiddle

Can you please help to understand this discrepancy?Thanks

I am using jquery visible selector to check if a child element is visible or not.But surprisingly .is("visible") & .is(":visible") is showing different result on using it with css visibility:hidden property.When I am using .is('visible') it is alerting false & on using .is(":visible") is it alerting true. But on changing the css property to display:none, the result is consistent.Here is the code.

HTML

<div id="parentEle">
    I have hidden span
    <span class="hiddenContent">
        I am hiddenContent
    </span>
</div>
<button type="button" onclick="_checkChild()">Check Child</button>

JS

function _checkChild(){
    var x= false;
    x =$("#parentEle").children(".hiddenContent").is(":visible");
    alert(x);
}

CSS

.hiddenContent{
   visibility:hidden
}

jsfiddle

Can you please help to understand this discrepancy?Thanks

Share asked Nov 7, 2015 at 8:41 brkbrk 50.4k10 gold badges59 silver badges84 bronze badges 2
  • 4 .is("visible") will check the selected tag is visible named tag – Pranav C Balan Commented Nov 7, 2015 at 8:44
  • 1 They are different selectors. I'm surprised that you think this is surprising. – GolezTrol Commented Nov 7, 2015 at 8:50
Add a ment  | 

3 Answers 3

Reset to default 9

.is(":visible"); is a jQuery special selector that checks if an element is visible which might vary from time to time. .is("visible"); is a standard selector that would check if the element is <visible /> and won't tell you anything about it's visibility.

I will explain you .is(":visible") will check that element is visible or not while .is("visible") will check that element is visible tag or not?

It means it will give true if you write like these in your code .is("span") because these text is span tag.

.is("visible"); will check for a <visible> tag, the correct syntax for checking an element's visibility is .is(":visible"); but an element is considered visible even with visibility: hidden or opacity: 0 because they still use space in the layout (see https://api.jquery./visible-selector/ ).

The only way to have .is(":visible")==false; is if an element doesn't use any space in the layout, which is what happens when an element's height and width are 0 or you use display: none (which, in the end, is the same as setting an element's width and height to 0)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744856063a4597412.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信