是否可以用双引号替换这些单引号?

时间:2022-09-15 16:21:54

I'm having some trouble trying to replace the single quotes ('...') with double quotes("...") in the following piece of code:

我在使用以下代码段中的双引号(“...”)替换单引号('...')时遇到了一些麻烦:

<img id="image" src="images/bird.jpg"  onmouseover="PlaySound('mySound'); this.src='images/bird_second_frame.jpg'" 
onmouseout="StopSound('mySound'); this.src='images/bird.jpg'">

Whenever I attempt to replace the single quotes with doubles, the code breaks, and I can't seem to find a different solution. I've been told not to use single quotes at all - is this an exception?

每当我尝试用双精度替换单引号时,代码就会中断,我似乎无法找到不同的解决方案。我被告知根本不使用单引号 - 这是一个例外吗?

Any help is appreciated.

任何帮助表示赞赏。

3 个解决方案

#1


4  

You can't use a literal " inside an HTML attribute value delimited by " characters. It will terminate the attribute value prematurely.

您不能使用文字“在由”字符分隔的HTML属性值内“。它会过早地终止属性值。

You can include one as &quot;, but that would make the code significantly harder to read.

您可以将其中一个包含为“,但这会使代码更难以阅读。

<img id="image" src="images/bird.jpg" onmouseover="PlaySound(&quot;mySound&quot;); this.src=&quot;images/bird_second_frame.jpg&quot;" onmouseout="StopSound(&quot;mySound&quot;); this.src=&quot;images/bird.jpg&quot;">

I've been told not to use single quotes at all - is this an exception?

我被告知根本不使用单引号 - 这是一个例外吗?

No. You've just been given bad advice.

不,你刚收到了不好的建议。

JavaScript and HTML don't distinguish between ' and " except when determining which characters can appear between them without being escaped.

JavaScript和HTML不区分'和',除非确定哪些字符可以出现在它们之间而不进行转义。

Using ' in your example is simply more readable.

在您的示例中使用'只是更具可读性。


A better approach would be to avoid inline JavaScript entirely though.

更好的方法是完全避免使用内联JavaScript。

<img id="image" 
     src="images/bird.jpg"
     data-sound="mySound"
     data-frame="images/bird_second_frame.jpg">

<script>
    var element = document.getElementById("image");
    element.addEventListener("onmouseover", play);
    element.addEventListener("onmouseover", stop);

    function play() {
        PlaySound(this.dataset.sound);
        this.dataset.original = this.src;
        this.src = this.dataset.frame;
    }

    function stop() {
        StopSound(this.dataset.sound);
        this.src = this.dataset.original;
    }
</script>

#2


3  

You can't use double quotes inside of a string that's bounded by double quotes because they would end the string.

你不能在由双引号限定的字符串中使用双引号,因为它们会结束字符串。

Your use of single quotes looks appropriate in this case.

在这种情况下,您对单引号的使用看起来很合适。

#3


1  

You can use single quotes freely in JavaScript; they're syntactically equivalent to double-quote characters (though any single string constant must be bound by the same kind of quote). That goes for HTML too, so you can get double quotes in your JavaScript by using single quotes for the attribute value delimiters:

您可以在JavaScript中*使用单引号;它们在语法上等同于双引号字符(尽管任何单个字符串常量必须由相同类型的引号绑定)。这也适用于HTML,因此您可以通过使用属性值分隔符的单引号在JavaScript中获取双引号:

<button onclick='alert("Hello World")'>Click Me</button>

However if you really want double quotes everywhere, you can escape them as HTML entities:

但是,如果你真的想在任何地方使用双引号,你可以将它们作为HTML实体转义:

<button onclick="alert(&quot;Hello World&quot;)">Click Me</button>

That's pretty ugly but it works: the HTML parser is specifically looking for quote characters.

这很丑陋但它确实有效:HTML解析器专门寻找引号字符。

The best thing to do is to stop embedding your JavaScript event handler setup in your HTML and do it purely with JavaScript.

最好的办法是停止在HTML中嵌入JavaScript事件处理程序设置,并使用JavaScript完全执行此操作。

#1


4  

You can't use a literal " inside an HTML attribute value delimited by " characters. It will terminate the attribute value prematurely.

您不能使用文字“在由”字符分隔的HTML属性值内“。它会过早地终止属性值。

You can include one as &quot;, but that would make the code significantly harder to read.

您可以将其中一个包含为“,但这会使代码更难以阅读。

<img id="image" src="images/bird.jpg" onmouseover="PlaySound(&quot;mySound&quot;); this.src=&quot;images/bird_second_frame.jpg&quot;" onmouseout="StopSound(&quot;mySound&quot;); this.src=&quot;images/bird.jpg&quot;">

I've been told not to use single quotes at all - is this an exception?

我被告知根本不使用单引号 - 这是一个例外吗?

No. You've just been given bad advice.

不,你刚收到了不好的建议。

JavaScript and HTML don't distinguish between ' and " except when determining which characters can appear between them without being escaped.

JavaScript和HTML不区分'和',除非确定哪些字符可以出现在它们之间而不进行转义。

Using ' in your example is simply more readable.

在您的示例中使用'只是更具可读性。


A better approach would be to avoid inline JavaScript entirely though.

更好的方法是完全避免使用内联JavaScript。

<img id="image" 
     src="images/bird.jpg"
     data-sound="mySound"
     data-frame="images/bird_second_frame.jpg">

<script>
    var element = document.getElementById("image");
    element.addEventListener("onmouseover", play);
    element.addEventListener("onmouseover", stop);

    function play() {
        PlaySound(this.dataset.sound);
        this.dataset.original = this.src;
        this.src = this.dataset.frame;
    }

    function stop() {
        StopSound(this.dataset.sound);
        this.src = this.dataset.original;
    }
</script>

#2


3  

You can't use double quotes inside of a string that's bounded by double quotes because they would end the string.

你不能在由双引号限定的字符串中使用双引号,因为它们会结束字符串。

Your use of single quotes looks appropriate in this case.

在这种情况下,您对单引号的使用看起来很合适。

#3


1  

You can use single quotes freely in JavaScript; they're syntactically equivalent to double-quote characters (though any single string constant must be bound by the same kind of quote). That goes for HTML too, so you can get double quotes in your JavaScript by using single quotes for the attribute value delimiters:

您可以在JavaScript中*使用单引号;它们在语法上等同于双引号字符(尽管任何单个字符串常量必须由相同类型的引号绑定)。这也适用于HTML,因此您可以通过使用属性值分隔符的单引号在JavaScript中获取双引号:

<button onclick='alert("Hello World")'>Click Me</button>

However if you really want double quotes everywhere, you can escape them as HTML entities:

但是,如果你真的想在任何地方使用双引号,你可以将它们作为HTML实体转义:

<button onclick="alert(&quot;Hello World&quot;)">Click Me</button>

That's pretty ugly but it works: the HTML parser is specifically looking for quote characters.

这很丑陋但它确实有效:HTML解析器专门寻找引号字符。

The best thing to do is to stop embedding your JavaScript event handler setup in your HTML and do it purely with JavaScript.

最好的办法是停止在HTML中嵌入JavaScript事件处理程序设置,并使用JavaScript完全执行此操作。