HTML/JavaScript 链接
这个问题以前可能有人问过,但我很难找到答案,因为我不太知道要使用什么搜索词。
在 HTML 中,如何使链接转到某个位置,但在单击时而不是执行 JavaScript?现在,我有一个有点像这样的链接:
<a href="#" onclick="javascript:dostuff()">Stuff</a>
但是,问题是,当有人右键单击该链接时,我希望他们能够将直接 URL 复制到该链接。另外,如果某人的浏览器不支持 JavaScript,或者他们禁用了 JavaScript,我仍然希望他们能够访问该页面。仅仅是
<a href="http://linkhere" onclick="javascript:dostuff()">Stuff</a>?
这样还是有更复杂的事情?
谢谢!
This question may have been asked before, but I had trouble finding an answer, since I didn't quite know what search terms to use.
In HTML, how do you make a link go a place, but execute JavaScript instead when it is clicked? Right now, I have a link that's somewhat like this:
<a href="#" onclick="javascript:dostuff()">Stuff</a>
But, the thing is, when someone right clicks the link, I want them to be able to copy a direct URL to the link. Also, if someone's browser does not support JavaScript, or they have it disabled, I would still like them to be able to reach the page. Is it just
<a href="http://linkhere" onclick="javascript:dostuff()">Stuff</a>?
Or is there something more complicated?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望脚本运行时不跟踪链接,请确保 dosuff 函数返回 false。
(“javascript:”毫无意义。它标记循环,但你没有循环。请参阅 HTML关于如何指定内在事件处理程序属性中使用哪种语言的规范 - 更好的是,使用 不显眼的 JavaScript 和渐进式增强)。
Make sure that the dosuff function returns false if you want the link to not be followed when the script runs.
(The 'javascript:' is pointless. It labels a loop, but you don't have a loop. See the HTML spec for how to specify which language is being used in the intrinsic event handler attributes — better yet, use unobtrusive JavaScript and progressive enhancement).
您需要让事件返回false来阻止默认操作。
You need to let the event return false to block the default action.