admin管理员组文章数量:1435859
I have the following structure of row in my table:
<tr class="mattersRow">
<td></td>
<td colspan="16">
<div class="dropdown">
<a href="#" data-toggle="dropdown">Choose QB</a>
<ul class="dropdown-menu" aria-labelledby="dLabel" role="menu"> … </ul>
</div>
</td>
</tr>
Note: I use bootstrap library
I need to set to every <td>
that has text inside "" (empty), or "Choose QB" class='danger'.
This is my jQuery code:
$("#tblMatters .mattersRow td").each(function () {
if ($(this).html() == "" || $(this).innerHTML == "Choose QB")
{
$(this).addClass("danger");
flag = true;
}
});
Of course, $(this).html() == ""
works great, but how to set "danger" class to the <td>
with Choose QB
I have the following structure of row in my table:
<tr class="mattersRow">
<td></td>
<td colspan="16">
<div class="dropdown">
<a href="#" data-toggle="dropdown">Choose QB</a>
<ul class="dropdown-menu" aria-labelledby="dLabel" role="menu"> … </ul>
</div>
</td>
</tr>
Note: I use bootstrap library
I need to set to every <td>
that has text inside "" (empty), or "Choose QB" class='danger'.
This is my jQuery code:
$("#tblMatters .mattersRow td").each(function () {
if ($(this).html() == "" || $(this).innerHTML == "Choose QB")
{
$(this).addClass("danger");
flag = true;
}
});
Of course, $(this).html() == ""
works great, but how to set "danger" class to the <td>
with Choose QB
4 Answers
Reset to default 3You may use :contains()
doc and :empty
doc selectors :
$('#tblMatters .mattersRow')
.find('td:contains(\'Choose QB\'),td:empty')
.addClass('danger');
Here is a demo : http://jsfiddle/wared/a84CW/.
addClass
accepts a function as an argument, and within that function you have access to the currently iterated td
element, and can check what it contains :
$("#tblMatters .mattersRow td").addClass(function() {
var txt = $.trim( $('a', this).text() );
return txt.length === 0 || txt == 'Choose QB' ? 'danger' : '';
});
If you're only trying to check the text inside the anchor, you should specify that in the question ?
Use $(this).text().trim() === 'Choose QB'
Try .closest()
$(this).closest('td').addClass("danger");
Use .filter()
$("#tblMatters .mattersRow td").filter(function () {
var txt = $.trim($(this).text());
return txt.length === 0 || txt === 'Choose QB';
}).addClass("danger");
or check text of anchor tag
$("#tblMatters .mattersRow td a").filter(function () {
var txt = $.trim($(this).text());
return txt.length === 0 || txt === 'Choose QB';
}).closest('td').addClass("danger");
This should work for both
$("#tblMatters .mattersRow td a").filter(function () {
var txt = $.trim($(this).text());
return $(this).closest('td').text().length === 0 || txt === 'Choose QB';
}).closest('td').addClass("danger");
本文标签: jqueryHow to get text of link that inside td in HTML Table using JavaScriptStack Overflow
版权声明:本文标题:jquery - How to get text of link that inside td in HTML Table using JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744525484a2610721.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论