我目前使用jQuery的<a>标签来启动像点击事件等事情。

例如<a href="#" class="someclass">Text</a>

但我讨厌“#”使页面跳转到页面顶部。我能做什么呢?


当前回答

你也可以在处理jquery后返回false。

Eg.

$(".clickableAnchor").live(
    "click",
    function(){
        //your code
        return false; //<- prevents redirect to href address
    }
);

其他回答

只使用

<a href="javascript:;" class="someclass">Text</a>

JQUERY

$('.someclass').click(function(e) { alert("action here"); }

为了防止页面跳转,你需要调用e.s stoppropagation ();在调用e.b epreventdefault()之后:

stopPropagation阻止事件沿着DOM树向上移动。更多信息请访问:https://api.jquery.com/event.stoppropagation/

您可以使用event.preventDefault()来避免这种情况。更多信息请点击:http://api.jquery.com/event.preventDefault/。

你可以只传递一个没有href属性的锚标记,并使用jQuery来执行所需的操作:

< class = " foo " > < / >的酒吧

你甚至可以这样写:

<a href="javascript:void(0);"></a>

我不确定这是一个更好的方法,但这是一个方法:)