Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jquery添加数据属性_Javascript_Jquery - Fatal编程技术网

Javascript 使用jquery添加数据属性

Javascript 使用jquery添加数据属性,javascript,jquery,Javascript,Jquery,我使用以下两行jquery生成了两个按钮: prevArrow: '<button type="button" data-role="none" class="slick-prev">Previous</button>', nextArrow: '<button type="button" data-role="none" class="slick-next">Next</button>', 我尝试在data role=none之后粘贴它,但当我

我使用以下两行jquery生成了两个按钮:

prevArrow: '<button type="button" data-role="none" class="slick-prev">Previous</button>',
nextArrow: '<button type="button" data-role="none" class="slick-next">Next</button>',

我尝试在data role=none之后粘贴它,但当我单击按钮时,它导致了404错误消息。我认为这与引号有关。提前感谢您的帮助。

您可以将数据设置为对象,然后使用JSON.stringify连接,因为它将转义所有引号:

var data = {
    "category": "Creamy-Delicious-Mashed-Potatoes",
    "action": "Ingredients",
    "label": "Get-Nutrition-Info"
} 

prevArrow: '<button data-ga-event="' + JSON.stringify(data) +'" >Previous</button>',

请显示您的尝试。显示的代码中没有任何内容会导致404,即使Strings中包含数据ga事件。这是我试图用它替换jquery行的内容,但当我单击按钮时,它会导致404错误消息。nextArrow:'Next'啊,我的错,你需要在JSON字符串中转义双引号,你是说这个吗?如果是这样的话,我试了一下,结果把整个旋转木马搞砸了:数据ga事件=“{\category\:\Creamy Delicious mash potatos\,\action\:\How to Videos\,\label\:\Left Nav\”
var data = {
    "category": "Creamy-Delicious-Mashed-Potatoes",
    "action": "Ingredients",
    "label": "Get-Nutrition-Info"
} 

prevArrow: '<button data-ga-event="' + JSON.stringify(data) +'" >Previous</button>',