【发布时间】:2015-04-05 10:47:32
【问题描述】:
我是 jQuery 新手,
我正在尝试做一些非常简单的事情。点击事件中的 if 语句。当我点击一个按钮时,如果变量 = 1,就会发生一些事情,如果它等于其他事情,就会发生其他事情。但似乎点击事件忽略了 if 语句。它只运行 if 语句中的任何内容。
我的代码:
HTML:
<button type="button" class="one">Press</button>
<button type="button" class="two">section 2</button>
<button type="button" class="three">section 3</button>
<div class="textbox">
<h1>hi</h1>
<p>text</p>
</div>
jquery:
var y = '.textbox p'
var n = 20
$('.one').click(function() {
if (n=1) {
$(y).html("hi hi <br> hi <br><br>");
$('.textbox h1').html("titulo");
var a=0
}
});
$('.two').click(function() {
if (n=1) {
$('.textbox p').html("hey <br> hi <br><br>");
$('.textbox h1').html("sup bro");
var a=0
}
else {
$('.textbox p').html("variable is not 1");
$('.textbox h1').html("another");
var a=0
}
});
即使我将变量设置为 20,click 事件也会运行 if 语句中的任何内容。就像它忽略了变量或 if 语句。
所以我不知道出了什么问题。发生了什么事?
【问题讨论】:
-
n=1是分配,而不是比较。转至n==1 -
您不是在比较 n,而是在分配 n,当分配给大于零的任何值时,它返回 true。
标签: jquery html variables if-statement jquery-click-event