简体   繁体   English

jQuery if / else语句不起作用

[英]jQuery if/else statements not working

Very new to jQuery, and I can't figure out why this little snippet of code isn't working. 对jQuery非常新,我无法弄清楚为什么这小段代码无法正常工作。 I set the background of the body to black, but I always return with the else statement, and my background turns orange instead of the desired pink. 我将主体的背景设置为黑色,但始终返回else语句,并且背景变为橙色而不是所需的粉红色。

$(document).ready(function(){
    $("body").css("background","black");
        if ($("body").css("background") == "black") {
            $("body").css("background", "pink");
        }
        else {
            $("body").css("background", "orange");
        }
});

You can see what's happening here: http://jsfiddle.net/jH6Y9/1/ 您可以在这里查看发生了什么: http : //jsfiddle.net/jH6Y9/1/

You can't just ask for a composite CSS style like "background"; 您不能只是要求复合CSS样式(例如“ background”); the value comes back as the empty string. 该值返回为空字符串。 Ask for "background-color". 要求“背景色”。 ( edit — Firefox gives me the empty string; other browsers might construct the whole composite set of background properties for you.) 编辑 -Firefox给了我空字符串;其他浏览器可能会为您构造整个组合的背景属性。)

Now, once you get past that problem, you'll discover that the reported color won't be "black". 现在,一旦解决了这个问题,您将发现报告的颜色不会是“黑色”。 It'll be some other color syntax that means "black". 这将是表示“黑色”的其他颜色语法。

Instead of relying on the style mechanism to keep track of what's going on, use a class instead: 与其依靠样式机制来跟踪正在发生的事情,不如使用一个类:

$("body").addClass("black");
if ($("body").hasClass("black"))
  $("body").addClass("pink");
else
  $("body").addClass("orange");

Then in your CSS: 然后在您的CSS中:

body.black { background-color: black; }
body.pink { background-color: pink; }
body.orange { background-color: orange; }

Better yet, use some semantically meaningful name for the state of the page instead of the color names, so that if you later change your mind and want to have the background switch from a dog to a kitten the code will still make sense. 更好的是,对页面状态使用一些在语义上有意义的名称,而不是颜色名称,这样,如果您以后改变主意,并且希望背景从狗变成小猫,该代码仍然有意义。

css 'background' gives you the full background param, it would look something like: CSS的“背景”给你完整的背景参数,它看起来像:

"rgb(0,0,0) none repeat scroll..."

If you just want to check the color, you need to specify that you want the color attribute, and also test it against the value that it actually uses, instead of "black" you actually want to test the rgb color value: 如果只想检查颜色,则需要指定所需的color属性,并针对其实际使用的值进行测试,而不是实际要测试的rgb颜色值,而不是“ black”:

if ($('body').css("background-color") == "rgb(0, 0, 0)")

The fix is simple. 解决方法很简单。 $("body").css("background-color", "black") !== "black" . $("body").css("background-color", "black") !== "black" Instead, it equals rgb(0, 0, 0) . 相反,它等于rgb(0, 0, 0) In CSS, "black" is not a real color. 在CSS中, "black"不是真实的颜色。 Instead, it gets converted to an rgb() color. 而是将其转换为rgb()颜色。 To fix it: 要解决这个问题:

$(document).ready(function(){
    $("body").css("background-color","black");
    console.log($("body").css("background-color"))
    if ($("body").css("background-color") == "rgb(0, 0, 0)") {
        $("body").css("background-color", "pink");
    }
    else {
        $("body").css("background-color", "orange");
    }
});

fiddle 小提琴

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

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