是否可以让 jQuery 找到一个对象的文本,然后找到它前面的链接,然后单击它?我有这个例子,我希望 jQuery 单击下面两个链接中的任何一个:
<tr>
<td nowrap="true" width="170" valign="top">
<div class="float-left">
<b><a href="javascript:GoToETURL('/url-example-1.html','business');">1234 Account</a></b>
<br>
<span style="display:none" class="fullaccnum">1234</span>
</div>
</td>
</tr>
<tr>
<td nowrap="true" width="170" valign="top">
<div class="float-left">
<b><a href="javascript:GoToETURL('/url-example-2.html','business');">5678 Account</a></b>
<br>
<span style="display:none" class="fullaccnum">5678</span>
</div>
</td>
</tr>
这是我目前正在使用的 jQuery,它可以正常工作,但是如果上述帐户的排序方式不同,那么它会点击错误帐户的错误链接:
if(account_id == '1234'){
jQuery(".float-left b a")[0].click(); //click on first href
} else {
jQuery(".float-left b a")[1].click(); //click on second href
}
那么 jQuery 是否有可能做这样的事情:
if(account_id == '1234'){
//find span class='fullaccnum' where span equals 1234
//found match, go back up the tree and click href
jQuery(".float-left b a").click();
//goes to 'url-example-1.html'
} else {
//find span class='fullaccnum' where span equals 5678
//found match, go back up the tree and click href
jQuery(".float-left b a").click();
//goes to '/url-example-2.html'
}
最佳答案
尝试使用这个功能
function clickonanchor(text){
$('tr').each(function(){
var spantext = $(this).find('.fullaccnum').text();
if (spantext == text){
$(this).find('.float-left > b > a').click();
}
});
}
并使用它
clickonanchor('1234');
关于javascript - jQuery - 单击基于 css 类和链接名称的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29909555/