jquery使用html查找元素

jquery使用html查找元素,jquery,Jquery,假设我有一些html,看起来像这样: <div id="TheTable"> <div class="TheRow"> <div class="TheID">123</div> <div class="TheQuantity">2</div> <div class="TheName">test1</div> </div> <div class=

假设我有一些html,看起来像这样:

<div id="TheTable">

  <div class="TheRow">
    <div class="TheID">123</div>
    <div class="TheQuantity">2</div>
    <div class="TheName">test1</div>
  </div>

  <div class="TheRow">
    <div class="TheID">456</div>
    <div class="TheQuantity">3</div>
    <div class="TheName">test2</div>
  </div>

</div>
function GetData(TheID) {

   var TheIndex = $('#TheTable').find("the index of the row that contains TheID")

   TheQuantity = $('#TheTable .TheQuantity').eq(TheIndex);
   TheName = $('#TheTable .TheName').eq(TheIndex);

}
   $('#TheTable .TheRow').each( function () {
       if ($(this).html() === TheID) { return $(this).index(); }
   });
我可以循环遍历每个.TheID并返回如下索引:

<div id="TheTable">

  <div class="TheRow">
    <div class="TheID">123</div>
    <div class="TheQuantity">2</div>
    <div class="TheName">test1</div>
  </div>

  <div class="TheRow">
    <div class="TheID">456</div>
    <div class="TheQuantity">3</div>
    <div class="TheName">test2</div>
  </div>

</div>
function GetData(TheID) {

   var TheIndex = $('#TheTable').find("the index of the row that contains TheID")

   TheQuantity = $('#TheTable .TheQuantity').eq(TheIndex);
   TheName = $('#TheTable .TheName').eq(TheIndex);

}
   $('#TheTable .TheRow').each( function () {
       if ($(this).html() === TheID) { return $(this).index(); }
   });
但我想知道是否有更直接的方法。如果你有什么建议,请告诉我


谢谢。

以下功能将有助于获取相应的数据:

function GetData(TheID) {

   var IDObj = $('#TheTable .TheID').filter(function(){
       return $.trim($(this).text()) == TheID;
   })

   TheQuantity = IDObj.siblings('.TheQuantity').text();
   TheName = IDObj.siblings('.TheName').text();

}

以下功能将有助于获取相应的数据:

function GetData(TheID) {

   var IDObj = $('#TheTable .TheID').filter(function(){
       return $.trim($(this).text()) == TheID;
   })

   TheQuantity = IDObj.siblings('.TheQuantity').text();
   TheName = IDObj.siblings('.TheName').text();

}

如果您能够向元素添加其他属性,将有助于提高搜索效率。例如,添加数据属性

 <div class="TheID" data-id="123">123</div>

 var rowIndex= $('.TheID[data-id="'+TheID+'"]').parent().index();
编辑:如果无法将属性添加到标记中,则使用filter方法的另一种方法是:

  var row= $('.TheID').filter(function(){
        return $(this).text()==TheID;                              
    }).parent();
  var name= row.find('.TheName').text();
  var qty=row.find('.TheQuantity').text();

如果您能够向元素添加其他属性,将有助于提高搜索效率。例如,添加数据属性

 <div class="TheID" data-id="123">123</div>

 var rowIndex= $('.TheID[data-id="'+TheID+'"]').parent().index();
编辑:如果无法将属性添加到标记中,则使用filter方法的另一种方法是:

  var row= $('.TheID').filter(function(){
        return $(this).text()==TheID;                              
    }).parent();
  var name= row.find('.TheName').text();
  var qty=row.find('.TheQuantity').text();
好的,谢谢。我将$(IDObj)编辑为IDObj,因为变量IDObj=$('#TheTable…有效地返回jquery对象。好的,谢谢。我将$(IDObj)编辑为IDObj,因为变量IDObj=$('#TheTable…有效地返回jquery对象。