How to resolve the conflict between single quotes and double quotes in Javascript

Source: Internet
Author: User

In JavaScript, improper use of single quotes and double quotes may result in js Code conflicts and JavaScript cannot run correctly. It is easy to resolve the conflict between single quotes and double quotes in JavaScript, we only need to use single-and-double-mixing, and we can escape all.

To solve the single-cited and double-cited conflicts in js, consider the following code:

The Code is as follows: Copy code

Html + = '<a onclick = "return removeOpenCss (' + e. point. lng + e. point. lat + ')"> cancel </a> ';

This is the code in js. If you write this code here, a js error will be prompted because the parameters in the removeOpenCss method do not have single or double quotes. If you write this code here:

The Code is as follows: Copy code

Html + = '<a onclick = "return removeOpenCss ("' + e. point. lng + e. point. lat + '")"> cancel </a> ';

An error is reported directly, because the single cited and double cited conflicts here, so I only solve this problem through:

The Code is as follows: Copy code

Html + = '<a onclick = "return removeOpenCss (& quot;' + e. point. lng + e. point. lat + '& quot;) "> cancel </a> ';

In JavaScript, an error occurs when two double quotation marks are nested.

The following solutions are available:

Replace the inner single quotes'
Replace double quotation marks with & quot;
The following is an example.

The Code is as follows: Copy code

<Html>
<Head>
<Title>
Javascript single quotes
</Title>
<Script>
Function showmSG (S)
{
Alert (S );
}
</Script>
</Head>
<Body>
<Input type = "button" value = "Click Me" onclick = "showmSG ('' & quot; FSDFDS ') "/>
<Input type = "text" id = "txtName" name = "txtName" value = "'& quot; FSDFDS"/>
</Body>
</Html>

Related Article

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.