JS string stitching in IE is the solution to the error _javascript skills

Source: Internet
Author: User
Long time no JS, a lot of things have forgotten. The recent use of JS stitching string encountered problems, no matter how the stitching in IE in the error, very depressed.

After a day, and picked up the code looked down, suddenly remembered in the Java concatenation string escape character, just think of JS also have this dongdong.

It turns out this way:
Copy Code code as follows:

TR + + "<td><a href= ' javascript:void (0); ' onclick= ' Confirmdeloneinfo (' +url2+ ' ', '" +obj.title+ '); ' > ';

In the editor is not an error, to the browser how to point onclick are not reflected, F12 on the error, looked at the script, the string completely changed

After modification:
Copy Code code as follows:

TR + + "<td><a href= ' javascript:void (0); ' onclick= ' Confirmdeloneinfo (\" "+url2+" \ ", \" "+obj.title+" \ "); ' > '

There's no problem.

Knowledge Summary:

The effect of single double quotes in JS is the same, but if you have a single quote in your string, you use double quotes outside, and if you have double quotes in your string, the outside is enclosed in single quotes, so you don't have to escape characters.

The output of the above result is:
Copy Code code as follows:

Onclick= ' Confirmdeloneinfo ("http://...?type=1&nodeId=11", "Test 1"); '

See in HTML, if the outside use of single quotes inside to use double quotes, or IE error, so use the escape character \ "" to convert double quotes. In the dynamic generation of HTML elements, pay particular attention to the issue of single double quotes, otherwise it will be a headache.

Oh, a little knowledge of the summary, welcome correction ....

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.