我有一个名为used的数组。我有一个函数在数组中添加代码,但检查代码是否已经添加。如果已添加它,则必须说“False”否则为True 问题是它会说错误和真实。 这是我的代码:
var used=[];//use that to check if the user has added the code to favourites again
var x='',y='',blur='',r="",g="",b="",op="",color="rgb(0,0,0)",shadow=''; //create the variables that will be used
function update(){
x=$("#x").val();
y=$("#y").val();
op=$("#o").val();
r=$("#red").val();
g=$("#green").val();
b=$("#blue").val();
blur=$("#blur").val();
shadow = x+"px "+y+"px ";
}
$(document).ready(function(){
$("#addC").click(function(){
if(used.indexOf(shadow) != -1){
alert("false");
}
else{
used.push(shadow);
alert("True");
}
});
$('.shadow-slider').change(function(){
update();
if (op != 1){
if(op.length > 3){op=op.substring(0, 3);}
color="rgba("+r+","+g+","+b+","+op+")";
}
else{
color="rgb("+r+","+g+","+b+")";
}
if (blur != 0){shadow += blur+"px ";}
shadow += color;
$('#object').css("text-shadow",shadow);
$('#code-output').html("text-shadow:"+shadow+";");
});
});
Shadow是一个全局变量,根据我页面的一些滑块而变化。有什么想法吗?
答案 0 :(得分:4)
我想你已经将点击处理程序绑定了两次,所以第一次找不到它而你得到第二种情况(并添加)但是第二次找到它(第一次添加)并显示第一个案例。