Ela*_*nda 2 javascript jquery twitter-bootstrap
我正在使用Twitter的Bootstrap顶部栏.
当我点击一些导航href
href=#SomeDivName
就像当你按下此页面中的导航时:
http://twitter.github.com/bootstrap/examples/fluid.html#contact
页面向下滚动了一下.
我希望点击后会显示相关部分,但我不希望页面向下滚动.
$('a[href="#SomeDiveName"]').on('click', false);
Run Code Online (Sandbox Code Playgroud)
当您单击href属性设置为的锚标记时,这将返回false #SomeDiveName.这将阻止元素的默认行为,在这种情况下,滚动到href属性引用的元素.
如果您有一组这些链接,那么我建议添加一个类来识别它们,以便您可以一次性选择它们:
$('.stop-this-link').on('click', false);
Run Code Online (Sandbox Code Playgroud)
stop-this-link只要单击该元素,这将阻止具有该类的所有元素的默认行为.按类选择要比属性快得多.按属性搜索时,必须检查DOM中的每个元素.
请注意,false在jQuery事件处理程序中返回与调用:event.preventDefault()和event.stopPropagation().
有关这些功能的更多信息,请参见此处:
event.preventDefault():http://api.jquery.com/event.preventdefaultevent.stopPropagation():http://api.jquery.com/event.stoppropagation