Html 如何在 JavaScript 中转义单引号 ( ' )?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16134910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 07:44:07  来源:igfitidea点击:

How do I escape a single quote ( ' ) in JavaScript?

javascripthtmlescaping

提问by Matthew

UPDATE:I want to give an updated answer to this question. First, let me state if you're attempting to accomplish what I have below, I recommend that you manage events by adding event listenersinstead. I highly recommend that you utilize jQueryfor your project and use their syntax to manage event listenersover using DOM.

更新:我想对这个问题给出更新的答案。首先,让我说明一下,如果您正在尝试完成我在下面所做的工作,我建议您改为通过添加事件侦听器来管理事件。我强烈建议你在你的项目中使用jQuery,并使用它们的语法来管理事件监听器而不是使用 DOM。

QUESTION

Okay, I am basically doing this:

好的,我基本上是这样做的:

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

I don't want double quotes (") where I put the \'. I only want a single quote, so I am trying to not make it put a double when it is used. I am trying to reach this in the final outcome.

我不想在我放 \' 的地方使用双引号 (")。我只想要一个单引号,所以我试图在使用它时不让它加双引号。我试图在最终结果中达到这一点.

<img src="something" onmouseover="change('ex1')" />

Escaping isn't working for me.

逃避对我不起作用。

My marked answer works fine, however, the cleaner (and more professional-looking way, IMO) is loganfsmyth's answer.

我标记的答案效果很好,但是,更干净(更专业的方式,IMO)是 loganfsmyth's answer

采纳答案by Niet the Dark Absol

You should always consider what the browser will see by the end. In this case, it will see this:

您应该始终考虑浏览器最终会看到什么。在这种情况下,它会看到:

<img src='something' onmouseover='change(' ex1')' />

In other words, the "onmouseover" attribute is just change(, and there's another "attribute" called ex1')'with no value.

换句话说,“onmouseover”属性只是change(,还有另一个ex1')'没有值的“属性” 。

The truth is, HTML does not use \for an escape character. But it does recognise &quot;and &apos;as escaped quote and apostrophe, respectively.

事实是,HTML 不\用于转义字符。但它确实分别识别&quot;&apos;作为转义引号和撇号。

Armed with this knowledge, use this:

有了这些知识,使用这个:

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

... That being said, you could just use JavaScript quotes:

...话虽如此,您可以只使用 JavaScript 引号:

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

回答by h2ooooooo

The answer here is very simple:

这里的答案很简单:

You're already containing it in double quotes, so there's no need to escape it with \.

您已经将它包含在双引号中,因此无需使用\.

If you want to escape single quotes in a single quote string:

如果要在单引号字符串中转义单引号:

var string = 'this isn\'t a double quoted string';
var string = "this isn\"t a single quoted string";
//           ^         ^ same types, hence we need to escape it with a backslash

or if you want to escape \', you can escape the bashslash to \\and the quote to \'like so:

或者如果你想转义\',你可以转义 bashslash\\和引用\'这样的:

var string = 'this isn\\'t a double quoted string';
//                    vvvv
//                     \ ' (the escaped characters)

However, if you contain the string with a different quote type, you don't need to escape:

但是,如果包含具有不同引号类型的字符串,则无需转义:

var string = 'this isn"t a double quoted string';
var string = "this isn't a single quoted string";
//           ^        ^ different types, hence we don't need escaping

回答by mburm

You can escape a 'in JavaScript like \'

你可以'像在 JavaScript 中转义 a\'

回答by loganfsmyth

Since the values are actually inside of an HTML attribute, you should use &apos;

由于值实际上在 HTML 属性内,您应该使用 &apos;

"<img src='something' onmouseover='change(&apos;ex1&apos;)' />";

回答by David Tayfun

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

OR

或者

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

It should be working...

它应该工作...