从表中的跨度类检索链接地址

Retrieve link address from Span Class in table

本文关键字:检索 链接 地址      更新时间:2023-09-26

我目前正在学习用javascript和jquery编码,所以我的知识非常差。我试图编辑一个由朋友用篡改猴子编写的脚本(有很多谷歌搜索),我假设我无法通过搜索找到答案,因为我一直在使用不正确的术语 - 但不知道它们是什么。

这是 HTML

<span class="village_anchor contexted" data-player="9281645" data-id="804"> 
<a href="/game.php?village=143&amp;screen=info_village&amp;id=804">Black Reaper (492|489) K44</a>  
<a class="ctx" href="#"></a></span>

我只想获取链接到的地址,然后使用位置。 去那里。

再次为我缺乏实力道歉,并感谢您的任何帮助。

您所要做的就是选择span class,然后找到您想要的a标签。所以你可以这样写:

var linkhref = $(".village_anchor").find("a").first().attr("href");

然后,您可以执行"重定向":

document.location = linkhref;

使用此代码。

var address = $('span.village_anchor.contexted a:eq(0)').attr('href');
window.location.href = address; //to open the link in same page.