下面小編就為大家帶來一篇jquery操作ul的一些操作筆記整理(乾貨)。小編覺得蠻不錯的,現在就分享給大家,也給大家做個參考。一起跟著小編過來看看吧
1、html標記
<ul id="attachText"> <li data-text="111"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >查看附件</a> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >删除</a></li> <li data-text="222"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >查看附件</a> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >删除</a></li> <li data-text="333"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >查看附件</a> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >删除</a></li> </ul>
JS
1、取得li屬性data-text的值用,隔開
function GetValues() { var values = ""; var obj = $("#attachText li"); if(obj.length>0) { var len = $(obj).length-1; $.each(obj, function (index, value) { //表示为最后一个元素 if (index == len) { values += $(value).attr("data-text"); } else { values += $(value).attr("data-text") + ","; } }) } console.log(values); }
輸出結果;111,222,333
#2、編輯的時候初始化ul的li項目
function LoadAttach() { $("#attachText").html("");//先清空 var data="111,222,333"; var arr = data.split(','); $.each(arr, function (index, value) { $("#attachText").prepend("<li data-value=\"" + $.parseJSON(data).data + "\"><a>查看附件</a> <a>删除</a></li>"); }); }
以上是jquery操作ul技巧總結的詳細內容。更多資訊請關注PHP中文網其他相關文章!