CSS:对内容中的字符进行编码后

发布于 2024-10-18 01:22:14 字数 322 浏览 9 评论 0原文

我正在使用以下CSS,它似乎有效:

a.up:after{content: " ↓";}
a.down:after{content: " ↑";}    

但是字符似乎不能像这样编码,因为输出是文字并显示实际字符串:

a.up:after{content: " ↓";}
a.down:after{content: " ↑";}   

如果我无法对其进行编码,感觉我应该使用一些东西例如 jQuery 中的 .append(),因为它支持编码。有什么想法吗?

I am using the following CSS, which seems to be working:

a.up:after{content: " ↓";}
a.down:after{content: " ↑";}    

The characters however cannot seem to be encoded like this, as the output is literal and shows the actual string:

a.up:after{content: " ↓";}
a.down:after{content: " ↑";}   

If I can't encode it, it feels like I should use something such as .append() in jQuery, as that supports the encoding. Any ideas?

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

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

发布评论

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

评论(3

離人涙 2024-10-25 01:22:14

要在 content 中使用编码的 Unicode 字符,您需要提供字符本身(就像您所做的那样),或其 UTF-8 转义序列而不是 HTML 实体:

a.up:after { content: " \2193"; }
a.down:after { content: " \2191"; }   

To use encoded Unicode characters in content you need to provide either the characters themselves (as you do), or their UTF-8 escape sequences instead of HTML entities:

a.up:after { content: " \2193"; }
a.down:after { content: " \2191"; }   
柠檬 2024-10-25 01:22:14

为什么要对这些字符进行编码?请记住,您正在编写 CSS,而不是 HTML。您的代码:

a.up:after{content: " ↓";}
a.down:after{content: " ↑";}

完全有效,只要您使用 UTF-8 编码保存文件并发送适当的标头:

Content-Type: text/css; charset=utf-8

编码字符仅在 HTML 中使用,以便内容和标签之间不会出现歧义。因此,您可以将 < 编码为 <,这样浏览器就不会认为它是标记的开头。像 这样的东西只是那些不知道如何使用 utf-8(或者出于某种原因不能使用)的人的商品:)。

Why do you want to encode those characters anyway? Remember, you're writing CSS, not HTML. Your code:

a.up:after{content: " ↓";}
a.down:after{content: " ↑";}

is perfectly valid, as long as you save the file with UTF-8 encoding and send the appropriate header:

Content-Type: text/css; charset=utf-8

Encoding characters is only used in HTML so that there is no ambiguity between content and tags. Thus, you would encode< as < so that the browser doesn't think it's the beginning of a tag. Stuff like are just commodities for people who don't know how to use utf-8 (or can't, for whatever reason) :).

书间行客 2024-10-25 01:22:14

只是想补充一下,如果您想通过attr() 函数,上面的方法不起作用。看

document.getElementById('wontwork').setAttribute('data-sym', ' \2714 ');
document.getElementById('willwork').setAttribute('data-sym', ' \u2714 ');
button::before {
  content: attr(data-sym);
}
* {
  font-size: 30px
}
<button id='wontwork' data-sym='to-be-replaced'>not rendered</button>
<button id='willwork' data-sym='to-be-replaced'>rendered !</button>

Just want to add that if you want to set dynamically the value of content via the attr() function, the above won't work. See

document.getElementById('wontwork').setAttribute('data-sym', ' \2714 ');
document.getElementById('willwork').setAttribute('data-sym', ' \u2714 ');
button::before {
  content: attr(data-sym);
}
* {
  font-size: 30px
}
<button id='wontwork' data-sym='to-be-replaced'>not rendered</button>
<button id='willwork' data-sym='to-be-replaced'>rendered !</button>

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