使用 jQuery 防止转到锚点 link
Prevent go to anchor link using jQuery
这是我的 jQuery 代码,当它读取 window.location.hash 时,它会向元素添加 CSS class 并显示出来。但我无法阻止去那个锚点。我试过 preventDefault() 但它似乎不起作用。
<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>
This is my site url,尝试点击任意一张幻灯片中的 'ABRIR COTIZADOR'。
谢谢!
感谢Alexander O'Mara,这是我找到的最佳答案:
el= $('#anchor_element');
id= el.attr("id");
el.removeAttr('id');
window.location.hash =id;
el.attr('id',id);
此选项可防止闪烁。归功于 Tobias Buschor, seen in Lea Verou Blog's comments
这是我的 jQuery 代码,当它读取 window.location.hash 时,它会向元素添加 CSS class 并显示出来。但我无法阻止去那个锚点。我试过 preventDefault() 但它似乎不起作用。
<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>
This is my site url,尝试点击任意一张幻灯片中的 'ABRIR COTIZADOR'。
谢谢!
感谢Alexander O'Mara,这是我找到的最佳答案:
el= $('#anchor_element');
id= el.attr("id");
el.removeAttr('id');
window.location.hash =id;
el.attr('id',id);
此选项可防止闪烁。归功于 Tobias Buschor, seen in Lea Verou Blog's comments