[英]Jquery select previous element
我是Jquery的新手,我已經查看了這里的答復,但找不到我問題的任何具體答案。
我有一個(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>
我正在嘗試做的是突出顯示* Not Found文本,然后禁用前面的href,以便無法單擊鏈接。
我開發了以下選擇器:
$('td')。highlight(' 未找到 ').each(function(){$(this).prev(“ a”)。removeAttr(“ href”)});;
高光選擇器有效,但removeattr無效。 語法可能不正確,但任何指針都將非常有用。
回答: -這個有效
$("td:contains('*Not Found*')").each(function(){$(this).parent().find('a').removeAttr("href")})
我個人建議:
// 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','#');
JS小提琴演示 。
作為選擇,而不是設置href
來#
,你可以只取出a
通過展開其內容元素:
$('td').filter(function(){
return $.trim($(this).text()) == '*Not Found*';
}).closest('tr').find('a').contents().unwrap();
JS小提琴演示 。
參考文獻:
attr()
。 closest()
。 contents()
。 filter()
。 find()
。 jQuery.trim()
。 text()
。 unwrap()
。 嘗試$(this).prev().find("a").removeAttr("href")}
也可能刪除鏈接無效。
嘗試用#
替換href
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.