我正在尝试使用文本"我的元数据"找到td旁边的表td中的文本。 我试图循环通过表tds等找到这个。是否有任何非常简单的方法来搜索td中的文本,该文本位于td旁边,具有某些值?
<table id="searchTable">
<tr>
<td>
My Metadata
</td>
<td>
I want to alert this td text which is next to td which has text My Metadata
</td>
</tr>
</table>
答案 0 :(得分:3)
您可以使用 :contains()
和 next()
:contains()
选择器,您可以获取包含特定文字的元素。next()
来获取旁边的兄弟姐妹。
var text = $('#searchTable td:contains(My Metadata)') // get td which contains the text
.next() // get sibling next to it
.text(); // get text content in it
console.log(text);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id="searchTable">
<tr>
<td>
My Metadata
</td>
<td>
I want to alert this td text which is next to td which has text My Metadata
</td>
</tr>
</table>
<小时/> 如果您想选择完全匹配的元素,请使用
filter()
var text = $('#searchTable td').filter(function() {
return $.trim($(this).text()) == 'My Metadata'; // check trimmed content and filter based on that
})
.next() // get sibling next to it
.text(); // get text content in it
console.log(text);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id="searchTable">
<tr>
<td>
My Metadata
</td>
<td>
I want to alert this td text which is next to td which has text My Metadata
</td>
</tr>
</table>
更新:如果html内容属于变量,那么您可以执行以下操作。
var content = `<table id="searchTable">
<tr>
<td>
My Metadata
</td>
<td>
I want to alert this td text which is next to td which has text My Metadata
</td>
</tr>
</table>`;
var text = $(content).find('td:contains(My Metadata)') // get td which contains the text
.next() // get sibling next to it
.text(); // get text content in it
console.log(text);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
答案 1 :(得分:1)
您可以使用:contains
选择器和next()
,如图所示。
$('#searchTable td:contains("My Metadata")').next().text()
:contains("My Metadata")
选择器将选择td
内#searchTable
My Metadata
,文字.next()
,.text()
将选择下一个节点,alert($('#searchTable td:contains("My Metadata")').next('td').text())
将提醒文字在td。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id="searchTable">
<tr>
<td>
My Metadata
</td>
<td>
I want to alert this td text which is next to td which has text My Metadata
</td>
</tr>
</table>
<script>
$(document).ready(function () {
//hide toolbar by default
$("#RestrictionsEditor").closest(".k-editor").find(".k-editor-toolbar").hide();
//bind the select event
$("#RestrictionsEditor").data("kendoEditor").bind("select", function(e){
//show toolbar on selection
$("#RestrictionsEditor").closest(".k-editor").find(".k-editor-toolbar").show();
});
//bind the blur event
$("#RestrictionsEditor").on("blur", function(e){
//hide toolbar
$("#RestrictionsEditor").closest(".k-editor").find(".k-editor-toolbar").hide();
});
});
</script>
答案 2 :(得分:1)
您可以使用:contains()
选择器选择包含指定字符串的所有元素作为innerText和next()
以获得元素的下一个兄弟元素。
$('td:contains("My Metadata")') // Select `<td>` that contains the passed string
.next() // Get the immediate next sibling
.text(); // Get inner text
alert($('td:contains("My Metadata")').next().text().trim());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="searchTable">
<tr>
<td>
My Metadata
</td>
<td>
I want to alert this td text which is next to td which has text My Metadata
</td>
</tr>
</table>