简体   繁体   中英

How to handle inline double quotation marks?

I have an a tag which i generate with JavaScript. I want to manipulate the href attribute by giving it a variable. But I have a syntax arror in my HTML, so its not valid. My first try:

'<a href="javascript: file_info(\'' + content_result[i][7] + '\', ' + content_result[i][0] + ')">Edit</a>'

And my second one:

'<a href=\'javascript: file_info("' + content_result[i][7] + '", ' + content_result[i][0] + ')\'>Edit</a>'

As you can see I used backslashes but I every time get an invalid html code.

如果您可以依靠ES6支持,那么对于模板字符串而言,这将是一项艰巨的任务。

`<a href="javascript: file_info('${content_result[i][7]}','${content_result[i][0]}')">Edit</a>`

What I do in these cases...

I "try" to always use the same "outer" quote.
Let's say, for now, that it's the double ones.

Then I decide if the second level occurs more often than the third.

So second level may be single.
But in your case, the third level occurs more often.

So escape the second level " . And use the single-quotes for the third level.

Always try to use "as-less-as-possible" the escaping... Since it makes the code less readeable.

There is no "absolute" way to do it. It depends on your code.

A trick: keep your + signs near your vars. For readability.
;)

So here would be your line:

"<a href=\"javascript: file_info('" +content_result[i][7]+ "', '" +content_result[i][0]+ "')\">Edit</a>"
'<a href="javascript: file_info(\'' + content_result[i][7] + '\', ' + content_result[i][0] + ')">Edit</a>'

My first one works fine. I just had implemented it in a different line for an another element :/

I am very sorry.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM