From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/web/api/css/escape/index.html | 125 ++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 files/zh-cn/web/api/css/escape/index.html (limited to 'files/zh-cn/web/api/css/escape') diff --git a/files/zh-cn/web/api/css/escape/index.html b/files/zh-cn/web/api/css/escape/index.html new file mode 100644 index 0000000000..3271798047 --- /dev/null +++ b/files/zh-cn/web/api/css/escape/index.html @@ -0,0 +1,125 @@ +--- +title: CSS.escape() +slug: Web/API/CSS/escape +tags: + - CSS + - escape() + - 参考 + - 方法 +translation_of: Web/API/CSS/escape +--- +

{{APIRef("CSSOM")}}{{SeeCompatTable}}

+ +

 CSS.escape() 静态方法返回 {{domxref("DOMString")}} 包含作为参数传递的转义字符串,主要用作CSS选择器的一部分。

+ +

语法

+ +
escapedStr = CSS.escape(str);
+
+ +

参数

+ +
+
str
+
The {{domxref("DOMString")}} to be escaped.
+
+ +

实例

+ +

基本结果

+ +
CSS.escape(".foo#bar")        // "\.foo\#bar"
+CSS.escape("()[]{}")          // "\(\)\[\]\\{\\}"
+CSS.escape('--a')             // "--a"
+CSS.escape(0)                 // "\30 ",  Unicode代码点“0”是30
+CSS.escape('\0')              // "\ufffd",  Unicode替换字符 
+ +

在上下文使用

+ +

要转义一个字符串作为选择器使用, escape()方法可以用于:

+ +
var element = document.querySelector('#' + CSS.escape(id) + ' > img');
+ +

escape()方法也可以用于转义字符串,它也转义了不严格需要转义的字符:

+ +
var element = document.querySelector('a[href="#' + CSS.escape(fragment) + '"]');
+ +

规范

+ + + + + + + + + + + + + + +
格式状态备注
{{SpecName('CSSOM', '#the-css.escape()-method', 'CSS.escape()')}}{{Spec2('CSSOM')}}Initial definition
+ +

浏览器兼容性

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatChrome(46.0)}}{{CompatGeckoDesktop("31")}}[1]{{CompatNo}}{{CompatNo}}{{CompatNo}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidAndroid WebviewFirefox Mobile (Gecko)IE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatNo}}{{CompatChrome(46.0)}}{{CompatGeckoMobile("31")}}[1]{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatChrome(46.0)}}
+
+ +

[1] Firefox 32做了一些小的修改,以符合规范和CSS语法的发展。标识符现在可以以' -- '开始,第二个破折号不能被转义。另外供应商标识符也不会被更改。

+ +

参见

+ + -- cgit v1.2.3-54-g00ecf