简体   繁体   中英

how to add single quotes and double quotes string in HTML5 data attributes

I need to append string something like this {"Key": "Value"} is got from API response which needs to be added to HTML data-attribute.

Problem: HTML parser changes single quote string to double quotes and ignore rest of the string. Display rest of the string in the HTML attribute color.

var baseDiv = $('#base');
var action = '{"key1": "Let's give a value to it "}';
var targetDiv = "<a href="javascript:void(0);"  data-action=\'' + JSON.stringify(action) + '\'></a>"
baseDiv.append(targetDiv);

这与HTML5无关,您对字符串序列有些迷茫,可能这是您需要的:

var targetDiv = '<a href="javascript:void(0);"  data-action="' + JSON.stringify(action) + '"></a>';
var baseDiv = $('#base'); 
var action = '{"key1": "Let\'s give a value to it "}'; 
var targetDiv = '<a href="javascript:void(0);" data-action="' + JSON.stringify(action) + '"></a>' 
baseDiv.append(targetDiv);

And with JQuery this is better:

var baseDiv = $('#base'); 
var action = '{"key1": "Let\'s give a value to it "}'; 
var targetDiv = $('<a href="javascript:void(0);">asdasd</a>');
targetDiv.attr('data-action', action);
baseDiv.append(targetDiv);

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