我是jQuery的新手,
我试着把事情做得很简单。单击事件中的if语句。当我单击一个按钮时,如果变量为= 1,就会发生一些事情,如果它等于其他东西,则会发生其他事情。但似乎click事件忽略了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,单击事件也会运行if语句中的任何内容。就好像它忽略了变量或if语句。
所以我不知道出了什么问题。发生什么事了呢?
发布于 2015-04-05 10:51:51
一个等号(=)设置变量的值。var foo = "bar";将foo设置为存储bar。
您需要使用两个等号(==),这是一个比较算子。(foo == "bar")将检查foo是否等于bar。
https://stackoverflow.com/questions/29456484
复制相似问题