Javascript 如何查找a中的所有内容<;td>;a<;th>;有具体的文字吗?
给定一个Javascript 如何查找a中的所有内容<;td>;a<;th>;有具体的文字吗?,javascript,jquery,Javascript,Jquery,给定一个表格如下: <tr> <th scope="row" style="padding-right: 1em;">Date</th> <td> April 26, 1937 <span class="noprint">; 80 years ago</span> <span style="display:none">&nb
表格
如下:
<tr>
<th scope="row" style="padding-right: 1em;">Date</th>
<td>
April 26, 1937
<span class="noprint">; 80 years ago</span>
<span style="display:none"> (<span class="bday dtstart published updated">1937-04-26</span>)</span><br>
16:30 – 19:30 (<a href="/wiki/Central_European_Time" title="Central European Time">CET</a>)
</td>
</tr>
在上面,我正在检查类.date
,但是如果它是
中的文本呢
jsiddle使用类.Location
:使用选择器查找包含给定文本的所有元素。结合查找td元素。将它们结合在一起:
$('th:contains("Date")') // Find th with text "Date" in it
.siblings('td') // Get sibling td
.find('.published') // Find something with class 'published'
.text().substring(0, 4) // Get first 4 charatecters of text
概念验证(启用控制台):您可以检查:
html:
检查JSFIDLE上的输出tareget或控制台
注意:只需在您的案例中使用.text()。嗨,谢谢,您能在JSFIDLE上详细说明一下吗,以备将来参考和更精确的回答?实际上,我只需要一年*
$('th:contains("Date")') // Find th with text "Date" in it
.siblings('td') // Get sibling td
.find('.published') // Find something with class 'published'
.text().substring(0, 4) // Get first 4 charatecters of text
<h2>TABLE DATA</h2>
<table id="testTable">
<tr>
<th scope="row" style="padding-right: 1em;">Date</th>
<td>
April 26, 1937
<span class="noprint">; 80 years ago</span>
<span style="display:none"> (<span class="bday dtstart published updated">1937-04-26</span>)</span><br>
16:30 – 19:30 (<a href="/wiki/Central_European_Time" title="Central European Time">CET</a>)
</td>
</tr>
</table>
<hr />
<h2>TARGET DATA</h2>
<p id="showOutPut"></p
$(document).ready(function(){
var content=$('#testTable tr').find('th').siblings('td').html();
$('#showOutPut').html(content);
console.log('Output : '+content);
});