简体   繁体   English

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

[英]Is it possible to replace these single quotes with double quotes?

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. 任何帮助表示赞赏。

You can't use a literal " inside an HTML attribute value delimited by " characters. 您不能使用文字"在由"字符分隔的HTML属性值内" It will terminate the attribute value prematurely. 它会过早地终止属性值。

You can include one as &quot; 你可以把一个包含为&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>

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. 在这种情况下,您对单引号的使用看起来很合适。

You can use single quotes freely in JavaScript; 您可以在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: 这也适用于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完全执行。

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

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