Processing character escaping with single double quotation mark in string assembling in JS

Source: Internet
Author: User

JS may be used to dynamically append elements, the data may also be transmitted from the background, of course, there are two ways of thinking,

1. Assemble in the background to return directly;

2. In the foreground JS inside assemble,

If you assemble large amounts of HTML, you may have a single double quotation mark that is prone to problems, so how to solve it? Recently encountered this problem, do a summary.

Can be assembled in a sentence.

Note: HTML element attributes are all enclosed in double-quoted strings with single quotes.

var htmlline= ' <tr id= 'row ' +obj.idarr+ '">";
Htmlline + = ' <td style= "text-align:left;padding-bottom:0px; > ';
Htmlline + = ' <div id="" style= "float:left;" > ';
Htmlline + = ' <div id= 'tdone_b_ ' +obj.idarr+ '"style="float:left">";
Htmlline + = ' <div id= 'img_add_1class= "leftcloseimg" onclick= "group.add_cate (' + obj.idarr+ '></div> ';
Htmlline + = ' </div> ';
Htmlline + = ' <div id="" style= "float:left" > ' + formdata.group_name + ' </div> ';
Htmlline + = ' </div> ';
Htmlline + = ' </td> ';
class= "tal" style= "padding-bottom:0px;text-align:center;" > ' + formdata.sort + ' </td> ';
Htmlline + = ' <td style= "text-align:center;padding-bottom:0px; > ';
class= "childcategory" style= "margin-left:46px;" > ';
Htmlline + = ' <a onclick= 'group.add (\ ' b\ ', ' +obj.idarr+ ')"href="javascript:void (0)"> Add sub-groups </a> ';
Htmlline + = ' <a onclick= 'group.edit (' +obj.idarr+ ')"href="javascript:void (0)"> Modify </a> ‘;
Htmlline + = ' <a onclick= 'group.dele (\ ' delp\ ', ' +obj.idarr+ ')"href="javascript:void (0)"> Delete </a> ';
Htmlline + = ' <a onclick= 'group.useconfirm (1, ' +obj.idarr+ ')"href="javascript:void (0)"> Enabled </a> ';
Htmlline + = ' </div> ';
Htmlline + = ' </td> ';
Htmlline + = ' </tr> ';

Htmlline + = ' <a onclick= ' Group.dele (\ ' delp\ ', ' +obj.idarr+ ') "href=" javascript:void (0) "> Delete </a>";

The onclick function uses a parameter as a string, so the escape character is added

It looks more neat and time-consuming to put together.

Processing character escaping with single double quotation mark in string assembling in JS

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.