简体   繁体   English

其他语句不起作用是我的语法在jquery上不正确?

[英]else statment not working is my syntax is incorrect on jquery?

Hi i am using if statement to change the opacity of a div on click event if a div is having the 0.5 opacity so it will change to 0 if its having 0 opacity so it will change to 0.5 i am using the following jquery code and it will change the opacity 0.5 to 0 but when i click again to change the opacity to 0 to 0.5 its not work. 嗨我正在使用if语句更改单击事件上div的不透明度如果div具有0.5不透明度,那么如果它具有0不透明度它将更改为0因此它将更改为0.5我使用以下jquery代码并且它将不透明度0.5更改为0但是当我再次单击将不透明度更改为0到0.5时它不起作用。 pls have a look on code 请看一下代码

$("#one_star").click(function(){
    if($(this).css({"opacity":"0.5"}))
    {
        $(this).css({"opacity":"0"});
    }
    else if(!$(this).css({"opacity":"0.5"}))
    {
        $(this).css({"opacity":"0.5"});
    }
});

is i am using a wrong syntax? 我使用的是错误的语法吗? any suggestion pls 任何建议请

The expression $(this).css({"opacity":"0.5"}) you have in if should return boolean true or false but it does not so else is not executing. 你在if中的表达式$(this).css({“opacity”:“0.5”})应该返回布尔值true或false,但不会执行其他操作。

try this, 试试这个,

if($(this).css("opacity") == "0.5")
 { 
        $(this).css({"opacity":"0"});
 }
 else 
 {
     $(this).css({"opacity":"0.5"});
 }

Your if 's are not actually checking the CSS opacity property. 你的if实际上并没有检查CSS opacity属性。

You need to do this: 你需要这样做:

if ( $(this).css('opacity') == "0.5" ) {
   //do stuff
}
if( $(this).css("opacity") == "0.5") ){
        $(this).css({"opacity":"0"});
} else {
        $(this).css({"opacity":"0.5"});
}

No need to check if it's not, just use the else clause: 不需要检查它是否是,只需使用else子句:

$("#one_star").click(function(){
    if($(this).css("opacity") === "0.5") {
        $(this).css({"opacity":"0"});
    } else {
        $(this).css({"opacity":"0.5"});
    }
});

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

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