简体   繁体   English

jQuery不会切换CSS属性?

[英]jQuery won't toggle css property?

I know there are tons of jquery css toggle threads, but I took my own approach, and I can't see why this doesn't work: 我知道有大量的jQuery CSS切换线程,但是我采用了自己的方法,因此看不到为什么不起作用:

 <script>
    $("#id").click(function(){ 
        if($("#example").css("visibility"=="hidden"))
        {
            console.log("if reached");
            $("#example").css( "visibility", "visible" );
            console.log($("#example").css("visibility"));
        }
        else
        {
            $("#example").css( "visibility", "hidden" );
            console.log("else reached");
            console.log($("#example").css("visibility"));
        }
    })
    </script>

Tried it in both FF and Chrome, and it simply doesn't work! 在FF和Chrome中都对其进行了尝试,但这根本不起作用!

Here's the HTML: 这是HTML:

<input id="id" type="text"></input>
<div id="example">Test Test Test </div>

Checking the color returned by .css('color') is not dependable, it way vary depending on browsers. 检查.css('color')返回的颜色是不可靠的,具体取决于浏览器。

Note that the computed style of an element may not be the same as the value specified for that element in a style sheet. 请注意,元素的计算样式可能与样式表中为该元素指定的值不同。 For example, computed styles of dimensions are almost always pixels, but they can be specified as em, ex, px or % in a style sheet. 例如,尺寸的计算样式几乎总是像素,但是可以在样式表中将它们指定为em,ex,px或%。 Different browsers may return CSS color values that are logically but not textually equal, eg, #FFF, #ffffff, and rgb(255,255,255). 不同的浏览器可能会返回逻辑上相等但文本上不同的CSS颜色值,例如#FFF,#ffffff和rgb(255,255,255)。

I think the best approach will be is to use a class like 我认为最好的方法是使用类似

 var $example = $("#example"); $("#id").click(function() { $example.toggleClass('red'); if ($example.hasClass('red')) { console.log('has red now'); } else { console.log('default color'); } }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input id="id" type="button"></input> <div id="example">Test Test Test</div> 

Try this one. 试试这个。

Html code: HTML代码:

<input id="id" type="button"></input>
<div id="example">Test Test Test </div>

Js code : js代码:

$(function(){   
    $("#id").click(function(){         
        if( $('#example').is(':visible') ) {
            $('#example').hide();
        } else {
            $('#example').show();
        }
    });
});

You're code is already there, just a syntax error, .css("visibility"=="hidden") evaluates to css(true) . 您的代码已经在那里,只是语法错误, .css("visibility"=="hidden")计算结果为css(true) You want to check the return, eg .css("visibility")=="hidden" 您想检查退货,例如.css("visibility")=="hidden"

 <script>
$("#id").click(function(){ 
    if($("#example").css("visibility")=="hidden"))
    {
        console.log("if reached");
        $("#example").css( "visibility", "visible" );
        console.log($("#example").css("visibility"));
    }
    else
    {
        $("#example").css( "visibility", "hidden" );
        console.log("else reached");
        console.log($("#example").css("visibility"));
    }
})
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM