Understanding of the use and escape of single quotation marks (') and double quotation marks (") in JS

Source: Internet
Author: User
Tags add time

I have been drawing pages over the past few days. As a developer, I want to draw pages because everything on the pages of the current project is obtained from the database, that is, dynamic, similar to designing our own personal QQ space, but because it is a large platform and has a custom business concept, all of them are more complicated and we will not talk about it here, what I call "Drawing pages" is how to combine data logic into HTML. The biggest problem I have encountered is what I want to talk about today;

There is a query on a page. Of course, the query conditions and field types are dynamic. Different text boxes should be generated based on the data type. If varchar2 is used, different fields will be generated, if date is used to generate the add time and select JS, there is a button to add a new condition group on the page to link to a JS event, which requires judgment on the text box, the judgment part is as follows;

If (tlist [Index]. value = "date ")
{
Table. rows [T]. cells [2]. innerhtml = '<input name = "' + MList [Index]. value + biduinum + '"type =" text "size =" 12 "style =" width: 200px "readonly =" readonly"Onclick = "wdatepicker ({datefmt:" yyyy-mm-dd "})" onfocus = "wdatepicker ({datefmt:" yyyy-mm-dd "})"Class = "wdate"/> ';
}
Else
{
Table. rows [T]. cells [2]. innerhtml = '<input name = "' + MList [Index]. value + biduinum + '"type =" text "size =" 12 "style =" width: 200px "/> ';
}

The focus is on the red part. In this way, I thought it was understandable.CodeOnce executed, JS errors are reported. The error message is "syntax error". js itself is a weak language. single quotation marks and double quotation marks can be confused if they are but used, however, if we use them together, we need to differentiate them. For example, we can replace single quotes and double quotes in the previous Code. However, if we only change one of them, we need to use the plus sign link, which has different meanings, for example

"'B'" Yes, but "" B "" will cause an error. "+" B "+" "is required ""

The problem cannot be solved through the change of single quotes and double quotes. I began to look for other ideas. I tried to use the \ (Escape Character) to solve the problem, as shown below:

Onclick = "wdatepicker ({datefmt: \" yyyy-mm-dd \"})"

But it still does not work. I personally think the reason is that JS is still in the role of HTML, rather than Java, so I thought of using & quot; to solve it,

Table. rows [T]. cells [2]. innerhtml = '<input name = "' + MList [Index]. value + biduinum + '"type =" text "size =" 12 "style =" width: 200px "readonly =" readonly"Onclick = "wdatepicker ({datefmt: & quot; yyyy-mm-DD & quot;})" onfocus = "wdatepicker ({datefmt: & quot; yyyy-mm-DD & quot ;})"Class = "wdate"/> ';

 

the problem is solved, for the first time in my life, I felt the magic of this thing. I used it only. It seems that in the Program world, everything has its own meaning. Even when we have to eliminate them, we need to know what they mean, because these meaningful things may be of great help to us at some point in time, and replacing their products may not be able to inherit all their advantages.

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.