JavaScript Elegant Simple stitching string

Source: Internet
Author: User

Objective

Recently maintained an old system, there is a large number of JS splicing string code, here a total of JS stitching string

JS Native string concatenation

The strings inside JavaScript can be spliced directly with +

Return "<a style= ' text-decoration:underline ' onclick= '" + valsar[1] + "return false;" + "' data-rowid= '" + row_id + "' href= ' javascript:; ' title= ' "+ valsar[0] +" > "+ valsar[0] +" </a> ";

The above is splicing a JS event with a tag, you can see the inside mixed with single quotation marks and double quotation marks, with relatively strenuous.

Because the single and double quotation marks are combined together, you can use the JS array to separate operations, and finally join up:

Data.push ("<a style= ' text-decoration:underline ' onclick= '");d Ata.push (valsar[1]);d Ata.push (' return false; '); Data.push ("' data-rowid= ');d Ata.push (row_id);d ata.push (" ' href= ' javascript:; ' title= ' ");d Ata.push (valsar[0]); Data.push (">");d ata.push (valsar[0]);d ata.push ("</a>");

This is actually not the same as the above method, just with the array will be separated by +

JS Template

All of these are older solutions, it is also laborious to use, and now are assembled with JS Template:

var strtem = "<a style= ' text-decoration:underline;color: #color # ' onclick= ' #click # return false; ' data-rowid= ' #rowId # ' href= ' javascript:; ' title= ' #title # ' > #title #</a> '; var values = {};values["color"] = color;values["click"] = valsar[1];values["title"] = valsar[0];values["rowId"] = Row_id;template (Strtem, values)
Summarize

JS splicing is now generally used template, here record the following

JavaScript Elegant Simple stitching string

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.