splice() 无法正常工作
我正在为每个单击的导航容器设置一个 cookie。
它设置一个连接的数组并设置 cookie 值。 如果再次单击,则将其从阵列中删除。
它不知何故有越野车。
它只有在点击其他元素后才会拼接。然后它的行为就很奇怪。
可能是 splice
不是正确的方法
var navLinkToOpen;
var setNavCookie = function(value){
var isSet = false;
var checkCookies = checkNavCookie()
setCookieHelper = checkCookies? checkCookies.split(","): [];
for(i in setCookieHelper){
if(value == setCookieHelper[i]){
setCookieHelper.splice(value,1);
isSet = true;
}
}
if(!isSet){setCookieHelper.push(value)}
setCookieHelper.join(",")
document.cookie = "navLinkToOpen"+"="+setCookieHelper;
}
var checkNavCookie = function(){
var allCookies = document.cookie.split( ';' );
for (i = 0; i < allCookies.length; i++ ){
temp = allCookies[i].split("=")
if(temp[0].match("navLinkToOpen")){
var getValue = temp[1]
}
}
return getValue || false
}
$(document).ready(function() {
$("#LeftNav li").has("b").addClass("navHeader").not(":first").siblings("li").hide()
$(".navHeader").click(function(){
$(this).toggleClass("collapsed").nextUntil("li:has('b')").slideToggle(300);
setNavCookie($('.navHeader').index($(this)))
return false
})
var testCookies = checkNavCookie();
if(testCookies){
finalArrayValue = testCookies.split(",")
for(i in finalArrayValue){
$(".navHeader").eq(finalArrayValue[i]).toggleClass("collapsed").nextUntil(".navHeader").slideToggle (0);
}
}
});
I am setting a cookie for each navigation container that is clicked on.
It sets an array that is joined and set the cookie value.
if its clicked again then its removed from the array.
It somehow buggy.
It only splices after clicking on other elements. and then it behaves weird.
It might be that splice
is not the correct method
var navLinkToOpen;
var setNavCookie = function(value){
var isSet = false;
var checkCookies = checkNavCookie()
setCookieHelper = checkCookies? checkCookies.split(","): [];
for(i in setCookieHelper){
if(value == setCookieHelper[i]){
setCookieHelper.splice(value,1);
isSet = true;
}
}
if(!isSet){setCookieHelper.push(value)}
setCookieHelper.join(",")
document.cookie = "navLinkToOpen"+"="+setCookieHelper;
}
var checkNavCookie = function(){
var allCookies = document.cookie.split( ';' );
for (i = 0; i < allCookies.length; i++ ){
temp = allCookies[i].split("=")
if(temp[0].match("navLinkToOpen")){
var getValue = temp[1]
}
}
return getValue || false
}
$(document).ready(function() {
$("#LeftNav li").has("b").addClass("navHeader").not(":first").siblings("li").hide()
$(".navHeader").click(function(){
$(this).toggleClass("collapsed").nextUntil("li:has('b')").slideToggle(300);
setNavCookie($('.navHeader').index($(this)))
return false
})
var testCookies = checkNavCookie();
if(testCookies){
finalArrayValue = testCookies.split(",")
for(i in finalArrayValue){
$(".navHeader").eq(finalArrayValue[i]).toggleClass("collapsed").nextUntil(".navHeader").slideToggle (0);
}
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
读作:
这个元素可能不是一个 int ,这会导致你的拼接失败,另外你必须检查该元素是否包含你想要拼接的位置,然后你必须在尝试之前检查它的值是否在 setCookieHelper 长度内拼接。
如果你想在给定位置进行拼接,你应该使用 for:
splice 需要一个索引,从哪里开始“拼接”以及“拼接”元素的数量。
reads as:
this element may not be an int and that causes your splice to fail, Also you must check if the element is contains the position where you want to splice then you must check if it's value is within the setCookieHelper lenght before you try to splice.
if you want to splice at a given position you should use a for:
splice expects an index where to begin "splicing" and the amount of "spliced" elements.