JS HTML single quote and double quotation mark

Source: Internet
Author: User

JS,There's no difference between single and double quotes, just look at your own habits.However, if you use double quotes in double quotation marks, you need to use backslashes to suppress the parsing of double quotes: alert("ABC\"def\ "ghi");
But the following situation needs to understand: In a page button, write the onclick event processing code, accidentally written as follows:
<inputtype="button"onclick="Alert("1 ")"/>-------------------incorrect IE prompt after the error, then casually changed to:
<inputtype="button"onclick="Alert(\"1\ ")"/>----------------not correct
The result is still wrong.at this time, I can not understand, although I know the most direct solution is written like this:
<inputtype="button"onclick="Alert(' 1 ')" />-------------------CorrectBut why is the escape character in JavaScript \ No effect?
This is because the code is still in the jurisdiction of the HTML, so the escape character should be HTML, not JavaScript.<inputvalue="double quotes"type="button"onclick="Alert(&quot; double quotes the;);" /> -------------------correct
<inputvalue="single quote"type="button"onclick="Alert(&apos; single quote the;);" />-------------------Correct
Summary
<inputValue="double quotes inside double quotes-Error"Type="button"onclick="Alert("OK ");"/>
<inputValue="Single quotation mark in outer single quote-error"Type="button"onclick='Alert('OK '); '/>
<inputValue="Two double quotes-error"Type="button"onclick="Alert(""OK" ");"/>
<inputValue="Two single quotes-error"Type="button"onclick="Alert("'Ok"');" />
<inputValue="Backslash \+ double quote-Error"Type="button"onclick="Alert(\"Ok\ ");"/>
<inputValue="Backslash \+ single quote-Error"Type="button"onclick="Alert(\' OK\ ');" />

<inputValue="outer double quotes single quote-ok"Type="button"onclick="Alert(' OK ');" />
<inputValue="Outer single quote double quotation mark-ok"Type="button"onclick='Alert("OK");'/>
<inputValue="External not using quotation marks-ok"Type="button"onclick=Alert(' OK '); />
Note: It is possible to transfer characters internally using HTML single or double quotes

From for notes (Wiz)

JS HTML single quote and double quotation mark

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.