【问题标题】:JQuery if statement not working as expected and == stop workingJQuery if 语句没有按预期工作并且 == 停止工作
【发布时间】:2012-12-18 11:35:09
【问题描述】:

此代码不起作用。曾经使用 source == "youtube" 但突然 == 甚至不工作或返回任何东西,当我这样做时,总是只有一个 ifs 得到批准,无论是 youtube 还是 vimeo,取决于你先放哪个编码。我检查了传递给函数的源代码,它很好。那么问题是什么?以及为什么 == 停止工作?

if (source = "youtube") {
    $("#videowrap article").html("<iframe src='http://www.youtube.com/embed/"+video_url+"' frameborder='0' allowfullscreen></iframe>");
    $("#videowrap article iframe").css({"width" : VideoWidth+"px","height" : VideoHeight+"px"});
    $("#videowrap article").css("marginLeft",MarginLeft+"px");
    $("#videowrap").fadeIn(500);
}
if (source = "vimeo") {
    $("#videowrap article").html("<iframe src='http://player.vimeo.com/video/"+video_url+"?badge=0' frameborder='0' webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>");
    $("#videowrap article iframe").css({"width" : VideoWidth+"px","height" : VideoHeight+"px"});
    $("#videowrap article").css("marginLeft",MarginLeft+"px");
    $("#videowrap").fadeIn(500);
}

【问题讨论】:

  • 我看不到足够多的代码来知道它为什么不起作用,但您必须使用== 来检查条件。如果不是,您将变量设置为该值,而不是检查变量的值。
  • 你试过else if==吗?
  • 您在这里所做的是将source 设置为等于“youtube”,无论source 的值如何,都将返回true,您需要使用== 或=== 进行比较,它不起作用,这意味着source 不等于“youtube”或“vimeo”尝试使用 firebug 或类似工具进行调试,看看此时它实际上是什么。

标签: jquery function if-statement


【解决方案1】:
if (source == "vimeo")

 if (source === "vimeo")

不是

if (source = "vimeo")

【讨论】:

    猜你喜欢
    • 2011-11-29
    • 1970-01-01
    • 2017-06-18
    • 2022-11-26
    • 2011-08-25
    • 2020-07-26
    • 2022-11-20
    • 2020-03-11
    • 1970-01-01
    相关资源
    最近更新 更多