我想要一个元素来触发页面上的下一个元素。
$("span.topic").click(function(){
$("span.topic").next("a.page").trigger('click');
});
<div>
<span class="topic">Topic 1</span>
<a class="page" href="http://www.google.com">Welcome to the first Topic</a>
</div>
答案 0 :(得分:1)
$('.topic').on('click', function(){
window.location.href = $(this).next('a').attr('href');
});
答案 1 :(得分:0)
使用this
因此它知道从哪个元素开始
$("span.topic").click(function(){
$(this).next("a.page").click();
});
或者如果跨度和锚点之间会有元素..
$("span.topic").click(function(){
$(this).nextAll("a").first().click();
});
答案 2 :(得分:0)
我不明白你为什么要使用这么多不必要的选择器。使用:
$("span.topic").click(function(){
$(this).next().click();
});
答案 3 :(得分:0)
如果您试图让<span class='topic'>Topic 1</span>
导航点击,您将无法通过触发锚标记的点击事件来实现。浏览器不会使用该方法进行导航。
然而,您可以将window.location.href
更改为httt://www.google.com
。
试试这个:
$("span.topic").click(function(){
window.location.href = $("span.topic").next("a.page").attr('href');
});
这是您正在尝试的解决方案,但引用您要导航到的网址并不是一种非常实用的方法。有没有理由你不会像这样在你的锚标签中包裹你?
<div>
<a class="page" href="http://www.google.com">
<span class="topic">Topic 1</span>
Welcome to the first Topic
</a>
</div>