【发布时间】:2014-03-28 08:54:34
【问题描述】:
我正在尝试制作一个可以添加和删除 <li> 元素的菜单栏。到目前为止一切顺利,但是当我尝试删除它们时,我遇到了问题。我已经玩了几个小时了,现在我想知道整个过程是否可以变得更容易(也许是一个对象?)。
无论如何,这是完整的代码(80 行),后面还有 cmets。
var tabs = $('.accountSelectNav');
var titles = [];
var listItems = [];
// when the page loads check if tabs need to be added to the ul (menu bar)
$(document).ready(function(e) {
if ($.cookie('listItems') != null) {
console.log('not null');
//return "listItems" to it's array form.
listItems = JSON.parse($.cookie('listItems'));
$('.accountSelectNav').append(listItems);
}
});
$('.selectTable td:first-child').on('click', function(e) {
$('#home_select').removeClass('navHighlight');
//grab the text value of this cell
title = $(this).text();
$.ajax({
url:'core/functions/getAccountId.php',
type: 'post',
data: {'title' : title}
}).fail (function() {
alert('error');
}).done(function(data) {
accountId = $.trim(data);
// store values in the cookie
$.cookie('account_id', accountId, {expires : 7});
$.cookie('title', title, {expires : 7});
window.location = ('home_table.php');
});
// make sure the value is NOT currently in the array. Then add it
var found = jQuery.inArray(title, titles);
if (found == -1) {
titles.push(title);
addTab();
}
// make sure the value is NOT currently in the array. Then add it
found = jQuery.inArray(title, listItems);
if (found == -1) {
addListItem();
//place <li>'s in cookie so they may be used on multiple pages
$.cookie('listItems', JSON.stringify(listItems));
};
});
$("body").on("click", ".deleteImage", function (e) {
var removeTitle = $(this).closest('li').find('a').text();
var removeItem = $(this).closest('li')[0].outerHTML;
//remove title from "titles" array
titles = jQuery.grep(titles, function (value) {
return value != removeTitle;
});
//remove <li> from "listItems" array
listItems = jQuery.grep(listItems, function (value) {
return value != removeItem;
});
// this shows the <li> is still in the listItemsarray
console.log(listItems);
// put the array back in the cookie
$.cookie('listItems', JSON.stringify(listItems));
removeTab(this);
});
$("body").on("mouseover", ".accountSelectNav li", function(e) {
$(this).find('.deleteImage').show();
});
$("body").on("mouseleave", ".accountSelectNav li", function(e) {
$(this).find('.deleteImage').hide();
});
function addTab() {
tabs.append('<li class="navHighlight">' + '<a href="#">' + title + '</a>' + '<a href="#">' + '<img src="images/delete.png" class="deleteImage"/>' + '</a>' + '</li>');
};
function removeTab(del) {
$(del).closest('li').remove();
}
function addListItem() {
var s = ('<li class="navHighlight">' + '<a href="#">' + title + '</a>' + '<a href="#">' + '<img src="images/delete.png" class="deleteImage"/>' + '</a>' + '</li>');
listItems.push(s);
}
所以你看我有两个长度相等的数组,它们应该总是相同的长度。一个存储要在选项卡中显示的标题,另一个保存<li> 的html,它将附加到<ul>。我从它的数组中删除标题没有问题。然而,从它的数组中删除<li> 变得相当麻烦。您会看到,当我得到 <li> 元素后,它被充气后,里面的 html 与放入的内容不完全匹配,浏览器添加了样式元素。
例如,变量“removeItem”代表我希望删除的选定<li>的html值。它看起来像这样:
<li class="navHighlight"><a href="#">Test1</a><a href="#"><img src="images/delete.png" class="deleteImage" style="display: inline;"></a></li>
但我的数组“listItems”中的值如下所示:
<li class="navHighlight"><a href="#">Test1</a><a href="#"><img src="images/delete.png" class="deleteImage"/></a></li>
所以我试图从我的数组中删除它总是失败,因为它们不是完美的匹配。
现在我的问题是如何删除这个<li> 项目?还有没有更简单的方法来完成整个过程而我只是没有看到它?
感谢您的宝贵时间。
编辑
按要求拨弄here
我能解释的最简单的方法。
单击指向小提琴的链接。
单击“应用名称”列中的任意单元格
这会将<li> 添加到表格上方的<ul>(菜单)
当您将鼠标悬停在 <li> 上时,会出现一张图片
点击图片
这应该从<ul>和数组listItems中删除<li>
现在没有
【问题讨论】:
-
是的,对不起。我尽量避免发布冗长的帖子,但我想如果我不包含所有细节,我最终会在这篇帖子中添加和编辑 100 次。
-
理解力不是我最好的品质......那是很多行
-
是的,在 jsfiddle.net 上做一个 jsfiddle,这样我们就可以很容易地看到问题。
-
@JamesG。小提琴准备好了
标签: javascript jquery arrays cookies