[英]Jquery select previous element
I am new to Jquery and I've had a look through the replies on here but I can't find any specific answers to my question. 我是Jquery的新手,我已经查看了这里的答复,但找不到我问题的任何具体答案。
I have a (ASP) generated table a snippet of which is: 我有一个(ASP)生成的表,其摘要为:
<a href="javascript:__doPostBack('gv2','Select$15')"
style="color:White;">S</a></td><td style="font-size:XX-Small;">1104</td>
<td style="font-size:XX-Small;">320.20.0116.090</td>
<td style="font-size:XX-Small;">*Not Found*</td>
What I'm trying to do is highlight the *Not Found text and then disable the preceeding href so the link cannot be clicked. 我正在尝试做的是突出显示* Not Found文本,然后禁用前面的href,以便无法单击链接。
I have developed the following selector:- 我开发了以下选择器:
$('td').highlight(' Not Found ').each(function(){$(this).prev("a").removeAttr("href")}); $('td')。highlight(' 未找到 ').each(function(){$(this).prev(“ a”)。removeAttr(“ href”)});;
The highlight selector works but the removeattr doesn't. 高光选择器有效,但removeattr无效。 Syntax is probably incorrect but any pointers would be very useful.
语法可能不正确,但任何指针都将非常有用。
Answered: - This works 回答: -这个有效
$("td:contains('*Not Found*')").each(function(){$(this).parent().find('a').removeAttr("href")})
I'd personally suggest: 我个人建议:
// selects all 'td' elements
$('td').filter(function(){
// retains only those whose text is precisely '*Not Found*'
return $.trim($(this).text()) == '*Not Found*';
// moves to the closest (ancestor) 'tr'
// finds the 'a' element within that 'tr' element
// sets the 'href' attribute to be equal to '#'
}).closest('tr').find('a').attr('href','#');
Alternatively, rather than setting the href
to #
, you could just remove the a
element by unwrapping its contents: 作为选择,而不是设置
href
来#
,你可以只取出a
通过展开其内容元素:
$('td').filter(function(){
return $.trim($(this).text()) == '*Not Found*';
}).closest('tr').find('a').contents().unwrap();
References: 参考文献:
attr()
. attr()
。 closest()
. closest()
。 contents()
. contents()
。 filter()
. filter()
。 find()
. find()
。 jQuery.trim()
. jQuery.trim()
。 text()
. text()
。 unwrap()
. unwrap()
。 try $(this).prev().find("a").removeAttr("href")}
尝试
$(this).prev().find("a").removeAttr("href")}
also removing the link might not work. 也可能删除链接无效。
try replacing the href with #
尝试用
#
替换href
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.