如何从用户制作的数组中专门拼接出一个项目?
How to specifically splice out an item from a user made array?
我正在尝试 add/remove 我的阵列中的特定项目仅使用一个按钮。
到目前为止,当我单击按钮时,它会将项目推入数组,如果我再次单击它会从数组中删除该项目。
我的问题是,如果我有更多按钮,每次都向我的数组添加不同的项目,再次单击任何按钮将完全清空数组。
我知道它是我的接头,它可能只是因为它是凌晨 1 点,但如果我能理解我做错了什么,我会被诅咒的。如果有人能告诉我正确的方法,那就太棒了。
这是我的示例代码,
var libary = Array();
Array.prototype.contains = function ( needle ) {
for (i in this) {
if (this[i] == needle) return true;
}
return false;
}
function btn1() {
if (libary.contains("Big Book")){
libary.splice("Big Book");
document.getElementById("1").style.cssText = "border: 0;border-radius:0;border-color:0;box-shadow:0;}";
}else{
libary.push("Big Book");
document.getElementById("1").style.cssText = "border: 5px solid #4099FF;border-radius: 12px;outline: none;border-color: #4099FF;box-shadow: 0 0 15px #4099FF;}";
}
};
function btn2() {
if (libary.contains("Small Book")){
libary.splice("Small Book");
document.getElementById("2").style.cssText = "border: 0;border-radius:0;border-color:0;box-shadow:0;}";
}else{
libary.push("Small Book");
document.getElementById("2").style.cssText = "border: 5px solid #4099FF;border-radius: 12px;outline: none;border-color: #4099FF;box-shadow: 0 0 15px #4099FF;}";
}
};
请记住,这只是一个示例,还有大约 20 个按钮。
我正在尝试 add/remove 我的阵列中的特定项目仅使用一个按钮。 到目前为止,当我单击按钮时,它会将项目推入数组,如果我再次单击它会从数组中删除该项目。 我的问题是,如果我有更多按钮,每次都向我的数组添加不同的项目,再次单击任何按钮将完全清空数组。
我知道它是我的接头,它可能只是因为它是凌晨 1 点,但如果我能理解我做错了什么,我会被诅咒的。如果有人能告诉我正确的方法,那就太棒了。 这是我的示例代码,
var libary = Array();
Array.prototype.contains = function ( needle ) {
for (i in this) {
if (this[i] == needle) return true;
}
return false;
}
function btn1() {
if (libary.contains("Big Book")){
libary.splice("Big Book");
document.getElementById("1").style.cssText = "border: 0;border-radius:0;border-color:0;box-shadow:0;}";
}else{
libary.push("Big Book");
document.getElementById("1").style.cssText = "border: 5px solid #4099FF;border-radius: 12px;outline: none;border-color: #4099FF;box-shadow: 0 0 15px #4099FF;}";
}
};
function btn2() {
if (libary.contains("Small Book")){
libary.splice("Small Book");
document.getElementById("2").style.cssText = "border: 0;border-radius:0;border-color:0;box-shadow:0;}";
}else{
libary.push("Small Book");
document.getElementById("2").style.cssText = "border: 5px solid #4099FF;border-radius: 12px;outline: none;border-color: #4099FF;box-shadow: 0 0 15px #4099FF;}";
}
};
请记住,这只是一个示例,还有大约 20 个按钮。