溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

jQuery插件zTree如何獲取一級節(jié)點(diǎn)數(shù)據(jù)

發(fā)布時(shí)間:2021-07-06 13:46:30 來源:億速云 閱讀:143 作者:小新 欄目:web開發(fā)

這篇文章主要介紹了jQuery插件zTree如何獲取一級節(jié)點(diǎn)數(shù)據(jù),具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

具體如下:

1、實(shí)現(xiàn)代碼:

<!DOCTYPE html>
<html>
<head>
  <title>zTree實(shí)現(xiàn)基本樹</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <link rel="stylesheet" type="text/css" href="zTree_v3/css/demo.css" rel="external nofollow" >
  <link rel="stylesheet" type="text/css" href="zTree_v3/css/zTreeStyle/zTreeStyle.css" rel="external nofollow" >
  <script type="text/javascript" src="zTree_v3/js/jquery-1.4.4.min.js"></script>
  <script type="text/javascript" src="zTree_v3/js/jquery.ztree.core.min.js"></script>
  <script type="text/javascript">
    <!--
    var setting = {
      data: {
        simpleData: {
          enable: true
        }
      }
    };
    var zNodes =[
      { id:1, pId:0, name:"湖北省", open:true},
      { id:11, pId:1, name:"武漢市", open:true},
      { id:111, pId:11, name:"漢口"},
      { id:112, pId:11, name:"漢陽"},
      { id:113, pId:11, name:"武昌"},
      { id:12, pId:1, name:"黃石市"},
      { id:121, pId:12, name:"黃石港區(qū)"},
      { id:122, pId:12, name:"西塞山區(qū)"},
      { id:123, pId:12, name:"下陸區(qū)"},
      { id:124, pId:12, name:"鐵山區(qū)"},
      { id:13, pId:1, name:"黃岡市"},
      { id:131, pId:13, name:"黃州區(qū)"},
      { id:132, pId:13, name:"麻城市"},
      { id:133, pId:13, name:"武穴市"},
      { id:134, pId:13, name:"團(tuán)風(fēng)縣"},
      { id:135, pId:13, name:"浠水縣"},
      { id:136, pId:13, name:"羅田縣"},
      { id:137, pId:13, name:"英山縣"},
      { id:2, pId:0, name:"湖南省", open:true},
      { id:21, pId:2, name:"長沙市", open:true},
      { id:211, pId:21, name:"芙蓉區(qū)"},
      { id:212, pId:21, name:"天心區(qū)"},
      { id:213, pId:21, name:"岳麓區(qū)"},
      { id:214, pId:21, name:"開福區(qū)"},
      { id:22, pId:2, name:"株洲市"},
      { id:221, pId:22, name:"天元區(qū)"},
      { id:222, pId:22, name:"荷塘區(qū)"},
      { id:223, pId:22, name:"蘆淞區(qū)"},
      { id:224, pId:22, name:"石峰區(qū)"}
    ];
    $(document).ready(function(){
      $.fn.zTree.init($("#baseTree"), setting, zNodes);
    });
    /**
     * 獲取全部節(jié)點(diǎn)數(shù)據(jù)
     * 一級節(jié)點(diǎn)數(shù)據(jù)
     */
    function findNodes()
    {
      var treeObj = $.fn.zTree.getZTreeObj("baseTree");
      //獲取全部節(jié)點(diǎn)數(shù)據(jù)
      var nodes = treeObj.getNodes();
      for (var i=0, l=nodes.length; i < l; i++)
      {
        var nodeId = nodes[i].id;
        var nodeName = nodes[i].name;
        alert("樹節(jié)點(diǎn)編碼:"+nodeId+"\n"+"樹節(jié)點(diǎn)名稱:"+nodeName);
      }
    }
    //-->
  </script>
 </head>
<body>
<div class="content_wrap">
  <div class="zTreeDemoBackground left" >
    <ul id="baseTree" class="ztree" ></ul>
    <input type="button" id="btn" onclick="findNodes()" value="獲取全部節(jié)點(diǎn)數(shù)據(jù)"/>
  </div>
</div>
</body>
</html>

2、實(shí)現(xiàn)效果圖:

(1)初始化

jQuery插件zTree如何獲取一級節(jié)點(diǎn)數(shù)據(jù)

(2)單擊“獲取全部節(jié)點(diǎn)數(shù)據(jù)”

jQuery插件zTree如何獲取一級節(jié)點(diǎn)數(shù)據(jù)

(3)單擊“確定”

jQuery插件zTree如何獲取一級節(jié)點(diǎn)數(shù)據(jù)

3、源碼說明

var treeObj = $.fn.zTree.getZTreeObj("baseTree");
//獲取全部節(jié)點(diǎn)數(shù)據(jù)
var nodes = treeObj.getNodes();

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“jQuery插件zTree如何獲取一級節(jié)點(diǎn)數(shù)據(jù)”這篇文章對大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI