我想要定义的超链接的href的位置“#”
问题描述:
我要定义的超链接的默认位置href "#"
我想要定义的超链接的href的位置“#”
<div class="main" id="#a">
<div class="child"> <a href="#">click</a>
</div>
</div>
我希望有一个情况,如果用户点击一个网址,由此,例如:
<a href="#">click</a>
他被重定向到div类id="#a"
。我希望该div类成为页面的默认顶部,而不是将用户带到页面的最顶部。
答
所以,你想改变的默认行为:
<a href="#">click</a>
所以它,而不是把你带到你的“主”分区:<div class="main">content</div>
你可以做什么用JavaScript和jQuery是写功能标识与HREF =“#”网页上的锚定体,并告诉他们执行你想要的行为,而不是默认的,就像这样:
// select a elements with href="#" and add event listener on click
$('a[href="#"]').on('click', function(e){
// prevent default behaviour of jumping to the top of the page
e.preventDefault();
// get target element position in pixels
target = $($('div.main')).position().top;
// animate a scroll effect to the pixel location
$('html,body').stop().animate({ scrollTop: target });
});
问题是问如何''#的意义从“页面顶部的”更改为“符合ID#A股利”。它没有要求如何在#后键入id。 – Quentin
谢谢@Quentin你真正理解我的问题.....我想改变一个href =“#”>的默认位置点击指向带有“main”类的div元素 – ebukaloper
是的,谢谢!希望这是更有帮助:) – Frish