如何使用jquery 的方法,将一段ul内a的title都一样的li去掉。

代码部分
<ul class="list-group">
<li class="list-group-item"><a title="Academic Writing">Academic Writing</a></li>
<li class="list-group-item"><a title="Academic Writing">Academic Writing</a></li>
<li class="list-group-item"><a title="Oral Presentation">Oral Presentation</a></li>
<li class="list-group-item"><a title="Oral Presentation">Oral Presentation</a></li>
</ul>

效果如图:
最新回答
干净好听的昵称

2024-11-05 00:30:44

$('ul li').not(function (i) {
    return $(this).prevAll('li:has(a[title="' + $("a', this).attr('title') + '"])').length < 1;
}).remove();
你好调皮

2024-11-05 00:14:53

jQuery(function($) {
    $("ul li a").each(function(i, dom) {
        var me = $(dom),
            t = me.attr("title");
        var ind = me.closest("li").index();
        $("ul li:gt(" + ind + "):has(a[title="" + t + "'])").remove();
    });
});
快乐若即若离

2024-11-05 00:15:30

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript" src="jquery-1.8.2.min.js"></script>
<script language="javascript">
$(document).ready(function () {
for(var i=0;i<$(".list-group").find("li").length;i++){
var obj=$(".list-group").find("li").eq(i).find("a").attr("title");
for(var j=i;j<$(".list-group").find("li").length;j++){
var cur=$(".list-group").find("li").eq(j).find("a").attr("title");
if(obj==cur){
$(".list-group").find("li").eq(j).remove();
}
}
}
});
</script>
</head>
<body>
<ul class="list-group">
<li class="list-group-item"><a title="Academic Writing">Academic Writing</a></li>
<li class="list-group-item"><a title="Academic Writing">Academic Writing</a></li>
<li class="list-group-item"><a title="Oral Presentation">Oral Presentation</a></li>
<li class="list-group-item"><a title="Oral Presentation">Oral Presentation</a></li>
<li class="list-group-item"><a title="Oral Presentation">Oral Presentation</a></li>
<li class="list-group-item"><a title="Academic Writing">Academic Writing</a></li>
</ul>
</body>
</html>