在我的可打印页面上使用 JavaScript 禁用链接标记
我有一个脚本,它通过复制 HTML 然后进行一些操作(例如在页面加载时禁用页面上的按钮)来创建可打印页面。
我还想禁用页面上的链接。我真的不介意它们是否看起来仍然像链接,只要它们不执行任何操作,并且不给出任何 JavaScript 错误! 锚标记似乎没有禁用属性...
不幸的是,我不能使用 jQuery,所以只能使用 JavaScript!
编辑:我想禁用页面上的链接、按钮等,以便当“可打印页面”在另一个窗口中打开时,用户不能通过单击按钮或链接来弄乱它。我希望它本质上是他们想要打印的页面的“冻结快照”。
I have a script that creates a printable page by copying the HTML across and then doing some manipulation, such as disabling the buttons on the page, on page load.
I also want to disable the links on the page. I don't really mind if they look like links still as long as they don't do anything, and don't give any JavaScript errors!
The anchor tag doesn't seem to have a disabled attribute...
Unfortunately, I can't use jQuery, so JavaScript only please!
Edit: I want to disable the links, buttons etc on the page so that when the 'printable page' opens in another window, the user cannot mess with it by clicking buttons or links. I want it to essentially be a 'frozen snapshot' of the page that they want to print.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将其 href 设置为 # 并覆盖
onclick
事件应该可以解决问题。Setting their href to # and overwriting the
onclick
event should do the trick.为什么不能使用 jQuery?好多了...
无论如何,这是一个正常的 javascript 方式。比上面小,你也不需要修改链接...通过定义 onclick 函数返回 false 它不会访问 href:
Why can't you use jQuery? So much nicer...
Anyway here is a normal javascript way. Smaller than above and you also don't need to modify the links... by defining the onclick function to return false it will not visit the href:
文档对象中还有一个链接数组。虽然我从未尝试过,但我相信您也可以设置它们。
There is also an array of links in the document object. While I've never tried, I believe you can set them too.