jQuery追加添加额外的qoute

时间:2015-07-17 22:54:46

标签: javascript jquery

我的代码:

$("#thescreenshots").append("<img class='screenshotthumbnails' 
    onclick='openScreenshotModal('"+dataReturned['screenshots'][i]+"')'
    src='/assets/screenshots/"+ dataReturned['screenshots'][i] +"'>");

正在输出:

<img class="screenshotthumbnails" onclick="openScreenshotModal(" 'img_0203.png')'="" 
    src="/assets/screenshots/IMG_0203.PNG">

为什么会增加额外的奇怪qoute?我该如何解决这个问题?

3 个答案:

答案 0 :(得分:2)

你需要逃避一些报价。试试这个:

$("#thescreenshots").append('<img class="screenshotthumbnails" onclick="openScreenshotModal(\''+dataReturned['screenshots'][i]+'\')" src="/assets/screenshots/'+ dataReturned['screenshots'][i] +'">');

它将输出如下内容:

<img class="screenshotthumbnails" onclick="openScreenshotModal('img_0203.png')" src="/assets/screenshots/IMG_0203.PNG">

答案 1 :(得分:1)

转义后在点击事件中使用双引号

onclick='openScreenshotModal(\"" + dataReturned['screenshots'][i] + "\")'

这可以确保该物业不会突然终止,因为外围物业以单一报价开头。

答案 2 :(得分:0)

最好将javascript与HTML分开 - 形式&amp;功能和所有......

尝试这样的事情:

$('body').on('click', '.screenshotthumbnails', function(){
  var largeImage = $(this).attr('data-larger-image');
  openScreenshotModal( largeImage );
});

var image = '<img class="screenshotthumbnails" data-larger-image="img_0203.png" src="/assets/screenshots/IMG_0203.PNG">';

$("#thescreenshots").append( image );