CSS3 - 如何在 Chrome 中的文本区域和输入中设置所选文本的样式?

发布于 2024-12-20 07:43:15 字数 455 浏览 5 评论 0原文

编辑:正如 @geca 在评论中指出的,这是一个已知的 WebKit bug。希望它能尽快修复!

::selection 伪元素允许设置所选文本的样式。这按预期工作,但不适用于 Google Chrome 15 中的文本区域和输入。 (我不确定这是否是 webkit 或 chrome 问题,因为我无法在 Linux 上使用 Safari。)

这是一个演示此问题的 jsfiddle: http://jsfiddle.net/J5N7K/2/ 段落中选定的文本具有应有的样式。文本区域中选定的文本和输入不是。 (但它是在 Firefox 上。)

我是否做错了什么,或者现在无法在 Chrome 上设置它的样式?

EDIT: As @geca noted in the comments, this is a known WebKit bug. Let's hope it gets fixed soon!

The ::selection pseudo-element allows one to style the selected text. This works as expected but not for textareas and inputs in Google Chrome 15.
(I'm not sure if it's a webkit or chrome issue since I can't use Safari on Linux.)

Here's a jsfiddle demonstrating this issue: http://jsfiddle.net/J5N7K/2/
The selected text at the pargraph is styled as it should be. The selected text in the textarea and input isn't. (But it is at Firefox.)

Am I doing something wrong or is it just not possible to style it at Chrome right now?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如若梦似彩虹 2024-12-27 07:43:15

带有 contenteditable

是一个选项吗?函数仅列出大多数内容的

演示: http://jsfiddle.net/ThinkingStiff/FcCgA/

HTML:

<textarea><textarea> Doesn't highlight properly in Chrome.</textarea><br />
<input value="<input> Doesn't highlight properly in Chrome." />
<p><p> Highlights just fine in Chrome!</p>
<div id="div-textarea" contenteditable><div contenteditable> Highlights just fine in Chrome!</div>

CSS:

textarea, input, p, div {
    width: 400px;
}

#div-textarea {
    -webkit-appearance: textarea;
    height: 32px;
    overflow: auto;
    resize: both;
}

::selection {
    background-color: black;
    color: white;
}

输出 (Chrome) :

在此处输入图像描述

Is a <div> with contenteditable an option? Functions just list a <textarea> for most things.

Demo: http://jsfiddle.net/ThinkingStiff/FcCgA/

HTML:

<textarea><textarea> Doesn't highlight properly in Chrome.</textarea><br />
<input value="<input> Doesn't highlight properly in Chrome." />
<p><p> Highlights just fine in Chrome!</p>
<div id="div-textarea" contenteditable><div contenteditable> Highlights just fine in Chrome!</div>

CSS:

textarea, input, p, div {
    width: 400px;
}

#div-textarea {
    -webkit-appearance: textarea;
    height: 32px;
    overflow: auto;
    resize: both;
}

::selection {
    background-color: black;
    color: white;
}

Output (Chrome):

enter image description here

绿萝 2024-12-27 07:43:15

这是一个已知的 WebKit 错误。抱歉,到目前为止还没有解决方案:)

更新:WebKit 错误已于 2014 年 10 月 13 日修复。

This is a known WebKit bug. Sorry, no solution thus far :)

Update: the WebKit bug was fixed on 10/13/2014.

玩套路吗 2024-12-27 07:43:15

您是否有可能不使用 CSS 伪元素而使用一些 jQuery。

看看这个http://jsfiddle.net/J5N7K/6/

如果您不明白 jQuery,请随时询问。

Is there any chance that instead of using CSS pseudo-element you can use some jQuery.

take a look at this http://jsfiddle.net/J5N7K/6/.

if you don't understand the jQuery feel free to ask about it.

超可爱的懒熊 2024-12-27 07:43:15

使用这个:

::-moz-selection {
  background: var(--blue);
  color: var(--white);
}

::selection {
  background: var(--blue);
  color: var(--white);
}

use this :

::-moz-selection {
  background: var(--blue);
  color: var(--white);
}

::selection {
  background: var(--blue);
  color: var(--white);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文