Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

javascript - HTML锚链接 - href和onclick两者?(HTML anchor link - href and onclick both?)

I want to author an anchor tag that executes some JavaScript and then proceeds to go wherever the href was taking it.(我想创建一个执行一些JavaScript的锚标签,然后继续前进到href所在的任何地方。)

Invoking a function that executes my JavaScript and then sets window.location or top.location to the href location doesn't work for me.(调用执行我的JavaScript然后将window.locationtop.location设置为href位置的函数对我来说不起作用。)

So, imagine I have an element with id "Foo" on the page.(所以,想象一下我在页面上有一个id为“Foo”的元素。)

I want to author an anchor similar to:(我想创作一个类似于的锚:)
<a href="#Foo" onclick="runMyFunction(); return false;">Do it!</a>

When this is clicked, I want to execute runMyFunction and then jump the page to #Foo (not cause a reload - using top.location would cause it to reload the page).(单击此按钮时,我想执行runMyFunction,然后将页面跳转到#Foo (不会导致重新加载 - 使用top.location会导致重新加载页面)。)

Suggestions?(建议?)

I am happy to use jQuery if it can help here...(我很高兴使用jQuery,如果它可以在这里帮助...)   ask by psychotik translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just return true instead?(只是return true ?)

The return value from the onClick code is what determines whether the link's inherent clicked action is processed or not - returning false means that it isn't processed, but if you return true then the browser will proceed to process it after your function returns and go to the proper anchor.(onClick代码的返回值决定了链接的固有点击操作是否被处理 - 返回false意味着它没有被处理,但是如果你返回true那么浏览器将在你的函数返回后继续处理它适当的锚。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...