我正在运行它并且有效:
$wikiDOM.find(".infobox th:contains('Date')")
但是,这也可能是Date of birth
,而我只需要使用字符串Date
定位那些onse。
更新
完整案例:
if ($wikiDOM.find(".infobox th:contains('Location')").length > 0 && $wikiDOM.find(".infobox th:contains('Date')").length > 0) {
$('#results').append('<li>' + curTitle + "<br>" + $wikiDOM.find('.infobox th:contains("Location")').siblings('td').text() + '<br>' + $wikiDOM.find('.infobox th:contains("Date")').siblings('td').text() + '</li>');
}
尝试:
$.fn.textEquals = function(txt) {
return $(this).text() == txt;
}
if ($wikiDOM.find(".infobox th").textEquals('Location') && $wikiDOM.find(".infobox th").textEquals('Date')) {
$('#results').append('<li>' + curTitle + "<br>" + $wikiDOM.find('.infobox th:contains("Location")').siblings('td').text() + '<br>' + $wikiDOM.find('.infobox th:contains("Date")').siblings('td').text() + '</li>');
}
答案 0 :(得分:1)
您可以使用JQuery's filter方法:
$(".infobox th").filter(function() {
return $(this).text() === "Date";
})
您的代码可以重构为(未经测试):
function doSelect(text) {
return $wikiDOM(".infobox th").filter(function() {
return $(this).text() === text;
})
}
if (doSelect('Location').length > 0 && doSelect('Date').length > 0) {
$('#results').append('<li>' + curTitle + "<br>" + doSelect("Location").siblings('td').text() + '<br>' + doSelect("Date").siblings('td').text() + '</li>');
}
答案 1 :(得分:1)
HTML
<table>
<thead>
<tr>
<th>test1</th>
<th>test2</th>
</tr>
</thead>
<tbody>
<tr>
<td>
</td>
<td>
</td>
</tr>
</tbody>
</table>
这是你的jquery
//I have tested below line and it worked
//$('th:contains("test1")').css('color', 'red')
var myth = $('th:contains("test1")'); //here is your th
答案 2 :(得分:0)
您可以使用.text()返回元素的字符串值,并按以下方式运行条件:
Html示例:
<th>My Text</th>
<th>My other Text</th>
jquery示例:
$('th').each(function()
{
if ($(this).text() === 'My Text') {
//do something
}
})
这将循环遍历所有元素,然后对文本值运行if语句。可在此处找到每项功能的信息: