![](/img/trans.png)
[英]Find td with specific text, and operate on the td right after that one?
[英]Find td with specific text, and operate on the ALL td's on its right?
這看起來有點類似於我的上一個問題( 找到具有特定文本的td,並在那之后的td上運行? ),但這實際上是不同的..
這次有一張這樣的桌子:
<table class="test">
<tr>
<td>Row 1</td><td>Text 1-1</td><td>Text 1-2</td> ...... <td>Text 1-n</td>
</tr>
<tr>
<td>Row 2</td><td>Text 2-1</td><td>Text 2-2</td> ...... <td>Text 2-n</td>
</tr>
<tr>
<td>Row 3</td><td>Text 3-1</td><td>Text 3-2</td> ...... <td>Text 3-n</td>
</tr>
<tr>
<td>Row 4</td><td>Text 4-1</td><td>Text 4-2</td> ...... <td>Text 4-n</td>
</tr>
<tr>
<td>Row 5</td><td>Text 5-1</td><td>Text 5-2</td> ...... <td>Text 5-n</td>
</tr>
</table>
基本上它有行,現在它有無限數量的列。
我需要使用特定文本右邊一個td
,然后在右邊的所有td
上操作。 要查找的文本將始終位於最左側的td
s,因為它將是行標題文本。
因此,例如,我可能想要找到帶有文本Row 3
的td
,然后我需要將xyz
附加到此td
右側所有td
的文本(其中包含文本Row 3
)。
所以在我的例子中,包含Row 3
的td
右邊的所有td
都會改變它們的文本:
Text 3-1 --> Text 3-1xyz
Text 3-2 --> Text 3-2xyz
.
.
.
Text 3-n --> Text 3-nxyz
我該如何解決這個問題?
還有其他類型的資源可以讓你選擇DOM上的元素,比如nextAll()
或siblings()
,基於你剛剛改變的代碼:
$(".test td:contains(Row 3)")
.nextAll().text(function(){
return $(this).text() + "xyz"
});
使用jquery,你可以使用包含選擇器,但它不適合,使用jQuery.each() 。
$("table.test td").each(function() {
if ($(this).html() == "Text 2-1")
$(this).append("xyz");
});
http://jsbin.com/IrEpeTE/1/edit
對於行使用“eq()”,得到第5行,如下所示:
$("table.test tr:eq(4) td").each(function() {
if ($(this).html()!="Row 5")
$(this).append("xyz");
});
要么 :
$("table.test tr td").each(function() {
if ($(this).parent().children().html()=="Row 5" && $(this).html()!="Row 5")
$(this).append("xyz");
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.