如何使用 CSS 或 JavaScript 禁用文本选择?
我正在制作一个 HTML/CSS/jQuery 画廊,有几个页面。
我确实有一个“下一步”按钮,它是一个带有 jQuery 单击侦听器的简单链接。
问题是,如果用户多次单击按钮,则会选择按钮的文本,然后选择整行文本。在我真正黑暗的设计中,这真的是丑陋且荒谬的。
所以这是我的问题:你能禁用 HTML 上的文本选择吗? 如果没有,我会非常怀念 Flash 及其在文本字段上的高级配置......
I am making a HTML/CSS/jQuery gallery, with several pages.
I indeed have a "next" button, which is a simple link with a jQuery click listener.
The problem is that if the user click the button several times, the text of the button is selected, and then the full line of text. In my really darky design, that is really ugly and nonsensical.
So here is my question: Can you disable text selection on HTML?
If not, I'll terribly miss flash and its high level of configuration on textfields...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
2017 年 1 月更新:
根据我可以使用,目前,除 Internet Explorer 9 及更早版本之外的所有浏览器都支持
用户选择
(但遗憾的是仍然需要供应商前缀)。所有正确的 CSS 变体是:
请注意,它是一个非标准功能(即不属于任何规范的一部分)。它不能保证在任何地方都能工作,并且浏览器之间的实现可能存在差异,并且将来的浏览器可能会放弃对其的支持。
更多信息请参阅 Mozilla 开发者网络文档。
UPDATE January, 2017:
According to Can I use, the
user-select
is currently supported in all browsers except Internet Explorer 9 and earlier versions (but sadly still needs a vendor prefix).All of the correct CSS variations are:
Note that it's a non-standard feature (i.e. not a part of any specification). It is not guaranteed to work everywhere, and there might be differences in implementation among browsers and in the future browsers can drop support for it.
More information can be found in Mozilla Developer Network documentation.
尝试使用此 CSS 代码来实现跨浏览器兼容性。
Try this CSS code for cross-browser compatibility.
您可以使用 JavaScript 来做您想做的事情:
肥皂盒:
您确实不应该以这种方式与客户端的用户代理搞混。如果客户想要选择文档上的内容,那么他们应该能够选择文档上的内容。如果您不喜欢突出显示颜色也没关系,因为您不是查看文档的人。
You can use JavaScript to do what you want:
Soap box:
You really shouldn't be screwing with the client's user agent in this manner. If the client wants to select things on the document, then they should be able to select things on the document. It doesn't matter if you don't like the highlight color, because you aren't the one viewing the document.
我不确定您是否可以将其关闭,但您可以更改它的颜色:)
然后只需将颜色与您的“深色”设计相匹配,看看会发生什么:)
I'm not sure if you can turn it off, but you can change the colors of it :)
Then just match the colors to your "darky" design and see what happens :)