使用jQuery防止转到锚点链接
这是我的jQuery代码,当它读取window.location.hash时,它会向元素添加一个CSS类,并显示出来。但我不能阻止我去那个锚地。我尝试了preventDefault(),但似乎不起作用使用jQuery防止转到锚点链接,jquery,scroll,anchor,preventdefault,Jquery,Scroll,Anchor,Preventdefault,这是我的jQuery代码,当它读取window.location.hash时,它会向元素添加一个CSS类,并显示出来。但我不能阻止我去那个锚地。我尝试了preventDefault(),但似乎不起作用 <script type="text/javascript"> function cotizar(n){ gotoSlide(n); $('#cotizadores > div').removeClass(); $('#cotizadores > div').e
<script type="text/javascript">
function cotizar(n){
gotoSlide(n);
$('#cotizadores > div').removeClass();
$('#cotizadores > div').eq(n).addClass("cotizador_activo");
}
$(document).ready(function() {
if(window.location.hash != ''){
var hash = window.location.hash.substring(1);
index = $(".tab-content > div ").children("#boton_"+hash).index();
cotizar(index);
clearInterval(slideival);
}
$(".slides > div > a").on('click',function(event){
window.location.hash = $(this).parent().attr('id');
index = $(this).parent().index();
cotizar(index);
event.preventDefault();
return false;
});
});
</script>
函数协同(n){
戈托利德(n);
$('#cotizadores>div').removeClass();
$('#cotizadores>div').eq(n).addClass(“cotizador_activo”);
}
$(文档).ready(函数(){
如果(window.location.hash!=''){
var hash=window.location.hash.substring(1);
index=$(“.tab content>div”).children(“#boton_uuz”+hash).index();
协作组(索引);
清除间隔(slideval);
}
$(“.slides>div>a”)。在('click',函数(事件){
window.location.hash=$(this.parent().attr('id');
index=$(this.parent().index();
协作组(索引);
event.preventDefault();
返回false;
});
});
,尝试在任何幻灯片中单击“ABRIR COTIZADOR”
谢谢 多亏了,这是我找到的最好的答案:
el= $('#anchor_element');
id= el.attr("id");
el.removeAttr('id');
window.location.hash =id;
el.attr('id',id);
此选项可防止闪烁。贷记到,见Try put
cotizar
函数中的文档ready可能重复