简体   繁体   English

你如何在JavaScript中使用多个引号?

[英]How do you use multiple quotes in javascript?

What do you do when you already have single and double quotes in a URL, but then you need to wrap that URL in quotes? 当您在URL中已经有单引号和双引号时,您会怎么做,但是您需要将该URL包装在引号中?

For example: 例如:

<script src="http://(some url text)xpath='//*[@id="node-1075"]/div/div[1]/div/div/p[2]'"></script>

The node ID is wrapped in quotes, if I put the URL alone in the address bar it works, but as soon as it gets wrapped in quotes it doesn't, I can't escape the quotes either or else it will fail, what do I do? 节点ID用引号括起来,如果我把URL单独放在它工作的地址栏中,但是一旦它被引号括起来就没有,我也无法逃脱引号,否则它会失败,什么我该怎么办?

You should be able to replace the double quotes with: %22 And the single quotes with: %27 您应该能够将双引号替换为:%22和单引号:%27

So your URL would be: 所以你的网址是:

"http://(some url text)xpath=%27//*[@id=%22node-1075%22]/div/div[1]/div/div/p[2]%27"

Here is the complete list of ASCII Encoding http://www.w3schools.com/tags/ref_urlencode.asp 以下是ASCII编码的完整列表http://www.w3schools.com/tags/ref_urlencode.asp

You need to escape them; 你需要逃脱它们; usually you write (inside HTML files) double quotes first, then single quotes (the opposite in .js files, but that's my personal style); 通常你先写(在HTML文件里面)双引号,然后是单引号(在.js文件中相反,但那是我的个人风格); whenever you need the same in between you need to escape it. 无论什么时候你需要相同的东西,你需要逃避它。

Example: 例:

document.getElementById("some").innerHTML = "<img src='something' onmouseover='change(\"ex1\")' />";

Notice that using the JavaScript escape character ( \\ ) isn't enough in an HTML context; 请注意,在HTML上下文中使用JavaScript转义字符( \\ )是不够的; you need to replace the double-quote with the proper XML entity representation, &quot; 你需要用适当的XML实体表示替换双引号, &quot; .

Example: 例:

<a href="#" onclick="doIt('The &quot;Thing&quot;');">Do It!</a>

In your case, I would recommend to URL encode "the URL" though. 在您的情况下, 我建议URL编码 “URL”。

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

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