2011-04-06 83 views
3

我有一个HTMLjQuery。获取标记+类名

<table> 
    <tbody> 
     <tr> 
      <td> 
       <input name="price" class="chrome-input" /> 
      </td> 
      <td> 
       <button name="sellButton" class="chrome-button"> 
       </button> 
       <button name="priceButton" class="chrome-button"> 
       </button> 
      </td> 
     </tr> 
    </tbody> 
</table> 

我怎样才能tr包含与chrome-button类内部控制。 像这样

alert($("tr+.chrome-button").html()); 

回答

4
$(".chrome-button").closest('tr') 
+0

这应该是最快的,因为伪选择器是通常要慢得多。 – Ben 2011-04-06 19:47:41

1

试试这个:

$("tr").children('td').each(function(){ 
    $(this).children('.chrome-button').each(function(){ 
     alert($(this).html()); 
    }); 
}); 
4

试试这个:

alert($("tr:has(.chrome-button)").html()); 

工作例如:http://jsfiddle.net/NAdr4/

+0

谢谢!解决的任务。对于教育,我想知道,如果有10 标签,如何获得第一个语法$(“tr:has(.chrome-button)”)[0] .html() – Tuco 2011-04-06 19:40:29

+0

您可以获得tr DOM元素由usinn [0]或.get(0)... – Chandu 2011-04-06 19:43:22

+0

$(“tr:has(.chrome-button)”)。eq(0) – Headshota 2011-04-06 19:44:09