如何通过控件创建嵌套循环以获取其属性(客户端)?

时间:2012-02-21 09:38:49

标签: javascript jquery asp.net html client-side

我有以下aspx:

<div id="columns" runat="server">
       <ul id="column1" class="column" >
        <!-- /////////////// -->
        </ul>
         <ul id="column2" class="column" runat="server">
            <li id="l91" class="widget color-red" runat="server" style=""> 
                     <div class="widget-head" style="cursor: move;">
                      <div class="edit-box" style="display: none;">
                      <div class="widget-content"> 
            </li>
        </ul>
        <ul id="column3" class="column" runat="server">
        </ul>
        <ul id="column4" class="column" runat="server">

        </ul>
    </div>

如何通过java脚本遍历div id="columns"以获取除ul之外的所有ul id="column1"然后循环遍历每个ul以获取每个{{1}的以下属性1}}:

id,class,此li ul的ID

我尝试在服务器端执行此操作但面临特定问题。所以我希望得到一些帮助,让这些信息客户端在数组中稍后将它们存储在数据库中

2 个答案:

答案 0 :(得分:3)

你需要使用not()方法从选择器中排除元素,试试这个:

$("#columns ul").not("#column1").each(function() {
    // do what you need with each ul here
    var ulId = $(this).attr("id");
    var ulClass = $(this).attr("class");

    $("li", $(this)).each(function() {
        // do what you need with each child li here
        var liId = $(this).attr("id");
        var liClass = $(this).attr("class");
    });
});

答案 1 :(得分:1)

<script type="text/javascript">
 function looping(){
  var topDiv = document.getElementById("columns");
  var uls = topDiv.getElementsByTagName("ul");
  for(ul in uls){
   if(uls[ul].id == "column1")continue;
   var lis = uls[ul].getElementsByTagName("li");
   for(li in lis){
    alert("Class: " + lis[li].className + " Id: " + lis[li].id);
   }
  }
 }
</script>