Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从表中的Span类检索链接地址_Javascript_Jquery - Fatal编程技术网

Javascript 从表中的Span类检索链接地址

Javascript 从表中的Span类检索链接地址,javascript,jquery,Javascript,Jquery,我目前正在学习用javascript和jquery编写代码,所以我的知识非常贫乏。我正试图编辑一个朋友用tampermonkey写的脚本(用谷歌搜索),我想我还没能通过搜索找到答案,因为我使用了不正确的术语,但不知道它们是什么 这是HTML <span class="village_anchor contexted" data-player="9281645" data-id="804"> <a href="/game.php?village=143&amp;scre

我目前正在学习用javascript和jquery编写代码,所以我的知识非常贫乏。我正试图编辑一个朋友用tampermonkey写的脚本(用谷歌搜索),我想我还没能通过搜索找到答案,因为我使用了不正确的术语,但不知道它们是什么

这是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>

我想做的就是获取链接到的地址,然后使用location。去那里

再次为我的能力不足表示歉意,并感谢您的帮助。

请使用此代码

var address = $('span.village_anchor.contexted a:eq(0)').attr('href');

window.location.href = address; //to open the link in same page.

您所要做的就是通过他的
class
选择
span
,然后找到所需的
a
标记。 所以你可以这样写:

var linkhref = $(".village_anchor").find("a").first().attr("href");
然后,您可以执行“重定向”:


这里您选择的是所有
a
标记。他只对第一个感兴趣。非常感谢-这项工作和我一样出色wanted@NewBee感谢他,如果它解决了你的问题,就接受它作为回答。对不起,我以为我已经这么做了,但现在已经这么做了。
document.location = linkhref;