我有这样的链接:
<a hreflang="15" class="comment-show-link" href="">View/hide comments</a>
Run Code Online (Sandbox Code Playgroud)
然后让这段代码显示/隐藏内容:
$('.comment-show-link').click(function(e) {
$('#comment-show-'+$(this).attr("hreflang")).slideToggle('slow');
e.stopPropagation();
});
Run Code Online (Sandbox Code Playgroud)
但是,当我点击链接时,页面被发送并且没有显示div"#comment-show-",为什么?我做错了什么?
您不希望stopPropagation(停止事件冒泡),您想要preventDefault(这会阻止事件的默认操作 - 在这种情况下,在链接之后).或者只是返回false从事件处理函数(这不都 [链接,向下滚动到开始与段落"返回false ......"] ).