我试图通过函数传递几个不同的案例
<div id="button1" class="button" onclick="pop(case1)">popup a</div>
然后我想在我的函数中创建不同的案例
function pop(a){
if (a='case1') {var mytext = "you pressed the popup case1"; var myclass=a;}
else if (a='case2'){var mytext = "you pressed the popup case2"; var myclass=a;}
else if (a='case3'){var mytext = "you pressed the popup case3"; var myclass=a;}
var p1 = document.createElement("div");
p1.classname =myclass;
p1.innerHTML=mytext;
document.body.appendChild(p1);
}
这是我为它制作的小提琴 http://jsfiddle.net/alexnode/WqsNJ/1/ 如何才能正确地创建条件?目前我总是获得case1弹出窗口。
答案 0 :(得分:1)
问题是您使用=
进行分配,而非使用==
进行核对,将=
更改为==
。< / p>
if (a=='case1') {var mytext = "you pressed the popup case1"; var myclass=a;}
else if (a=='case2'){var mytext = "you pressed the popup case2"; var myclass=a;}
else if (a=='case3'){var mytext = "you pressed the popup case3"; var myclass=a;}
答案 1 :(得分:1)
为了比较JavaScript中的字符串,您需要使用is 等于运算符(==
)而不是等号(=
)< /强>
所以正确的代码应该是
function pop(a){
if (a=='case1') {var mytext = "you pressed the popup case1"; var myclass=a;}
else if (a=='case2'){var mytext = "you pressed the popup case2"; var myclass=a;}
else if (a=='case3'){var mytext = "you pressed the popup case3"; var myclass=a;}
var p1 = document.createElement("div");
p1.classname =myclass;
p1.innerHTML=mytext;
document.body.appendChild(p1);
}