2011-04-06 79 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