可能重复:
Remove li tags 2
我正在搜索解决方案,以删除使用javascript数组过滤的<li>
个标记。
阵列:
var mygroups=["1","8","3","4","5"]
示例(输入):
<li><div>1 element.</div></li>
<li><div>2 element. This is my <span>post</span> into new group</div></li>
<li><div>3 element. Another one</li>
<li><div>
<a href="javascript:void(0);">Actor</a>
<a href="/groups/viewgroup/1-first-group">First group</a>
</div>
</li>
<li><div><a href="javascript:void(0);">Actor</a>
<a href="/groups/viewgroup/10-ten-group">Ten group</a>5 element. This is my <span>new post</span></div></li>
<li>
<div>6 element. <a href="/j1.5/index.php/jomsocial/63-zzz/profile">Actor</a></div>
<div><a href="/groups/viewgroup/test/5-second-group">Group other</a></div>
</li>
<li>7 element.</li>
<li><div><a href="/groups/viewgroup/test/8-second-group">First group</a></div></li>
<li><div><a href="/groups/viewgroup/16-other-group">First group</a></div></li>
<li><div><a href="/j1.5/index.php?option=com_community&view=groups&task=viewgroup&groupid=1&Itemid=4">My other group </a></div></li>
如何获取此输出(删除锚中数组中不包含group的所有实例):
<li><div>
<a href="javascript:void(0);">Actor</a>
<a href="/groups/viewgroup/1-first-group">First group</a>
</div>
</li>
<li>
<div>6 element. <a href="/j1.5/index.php/jomsocial/63-zzz/profile">Actor</a></div>
<div><a href="/groups/viewgroup/test/5-second-group">Group other</a></div>
</li>
<li><div><a href="/groups/viewgroup/test/8-second-group">First group</a></div></li>
<li><div><a href="/j1.5/index.php?option=com_community&view=groups&task=viewgroup&groupid=4&Itemid=4">My other group </a></div></li>
此代码无效:
$('li').filter(function() {
var a = $(this).find('a');
if (!a.length)
return true;
var text = a.attr('href').split('/')[3];
text = text.substring(0, text.indexOf('-'));
if ($.inArray(text , mygroups) >= 0)
return false;
return true;
}).remove();
而且:
$('li').filter(function() {
if($(this).is('*:has(a[href|=/group/viewgroup/])') {
var href = $(this).find('a').attr('href'),
trail = href.split('/')[3],
number = + /(\d+)-/.exec(trail)[1];
return $.inArray(myarray, +number) == -1;
}
return true;
}).remove();