简体   繁体   English

在CSS选择器中转义双引号

[英]Escaping double quotes in CSS selector

I have a html tag, 我有一个html标签,

<div style="background-image: url("resources/images/test.jpg");"/>

I want to target this element using CSS selector. 我想使用CSS选择器定位此元素。 I tried below selector but its throwing error, 我在选择器下面尝试过,但是它抛出错误,

document.querySelector('*[style*="background-image: url("resources/images/test.jpg")"]')

Error: DOM Exception: SYNTAX_ERR (12)

I somehow have to escape double quotes around "resources/images/test.jpg". 我必须以某种方式在“ resources / images / test.jpg”周围转义双引号。 Please let me know if there is anyway I can escape it and target the tag. 请让我知道是否仍然可以将其转义并定位标记。

The issue is not just in your selector string but also in your markup. 问题不仅存在于选择器字符串中,还存在于标记中。 The quotes in the url() value are interfering with the style attribute value resulting in invalid HTML. url()值中的引号会干扰style属性值,从而导致HTML无效。 ( The /> bit is invalid as well but that's not really relevant.) />位置也是无效的,但这并不重要。)

Since you're dealing with a url() value you can simply drop the quotes: 由于您要处理url()值,因此可以简单地删除引号:

<div style="background-image: url(resources/images/test.jpg);">

Then amend your selector accordingly: 然后相应地修改选择器:

document.querySelector('*[style*="background-image: url(resources/images/test.jpg)"]');

On a side note I highly recommend trying to identify the element some other way, eg by using a class or constructing a selector based on the structure of your markup, instead of targeting the style attribute, which is fragile. 另外,我强烈建议尝试以其他方式标识元素,例如通过使用类或基于标记的结构构造选择器,而不是针对脆弱的style属性。 If you still need to test this style, you should just query the element's computed styles using window.getComputedStyle() on the element after selecting it, since it's very likely that the inline style will be used. 如果仍然需要测试此样式,则只需在选择元素后使用window.getComputedStyle()来查询元素的计算样式,因为很有可能会使用内联样式。

If you cannot edit the markup, and what you have given in your question is exactly how it appears, then unfortunately you have invalid markup and it will be impossible for you to match this element, even with a valid selector. 如果您无法编辑标记,而您在问题中给出的正是标记的显示方式,那么很遗憾,标记无效,即使使用有效的选择器,也无法匹配该元素。


If you absolutely must have the double quotes in the url() value, you have a few options: 如果绝对必须在url()值中使用双引号,则可以选择以下几种方法:

  • HTML-encode the inner quotes: HTML编码内引号:

     <div style="background-image: url(&quot;resources/images/test.jpg&quot;);"> 
  • Or swap the outer double quotes with single quotes so you don't have to HTML-encode anything: 或将外部双引号替换为单引号,这样就无需对HTML进行编码:

     <div style='background-image: url("resources/images/test.jpg");'> 

To use the double quotes within the attribute selector: 要在属性选择器中使用双引号,请执行以下操作:

  • You need to escape them twice: once for the selector, and again so it doesn't break the JavaScript string: 您需要对它们进行两次转义:一次是用于选择器,另一次是为了不破坏JavaScript字符串:

     document.querySelector('*[style*="background-image: url(\\\\"resources/images/test.jpg\\\\")"]'); 
  • Alternatively, you can swap the quotes as above, although you will still need to escape the inner double quotes once so they don't break the string: 另外,您也可以如上所述交换引号,尽管您仍然需要对内部双引号进行一次转义,以便它们不会破坏字符串:

     document.querySelector("*[style*='background-image: url(\\"resources/images/test.jpg\\")']"); 

You can also use single quotes in the url() value, in which case you would need to follow the steps above, swapping the quotes as appropriate (except there is no need to HTML-encode anything). 您还可以在url()值中使用单引号,在这种情况下,您需要按照上述步骤操作,并根据需要交换引号(除非不需要对HTML进行编码)。 But as you can see, it's extremely complicated; 但是正如您所看到的,它非常复杂; if you can edit the markup, I'd honestly just drop the quotes entirely and avoid all of this. 如果您可以编辑标记,老实说,我会完全删除引号,并避免所有这些。

Try doing this instead: 尝试这样做:

<div style="background-image: url(resources/images/test.jpg);"></div>

you don't need to have the quotes around the file path of your image 您不需要在图像的文件路径周围加上引号

您不需要使用引号,但是,如果需要使用引号,则始终可以使用单引号'

The div tag that shows up in HTML is not under my control so I can't remove the quotes in url. HTML中显示的div标签不受我的控制,因此我无法删除url中的引号。 Anyway based @Pedro comments below selector worked for me, 无论如何,选择器下面基于@Pedro的评论对我有用,

document.querySelector("*[style*='background-image: url(\"resources/images/test.jpg\")']")

I'm writing scripts in Selenium so it is necessary for me to target the image divs based on its source. 我正在用Selenium编写脚本,因此有必要根据图像源确定图像div。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM