如何在“”javascript中的特定变量值

时间:2016-09-07 03:52:21

标签: javascript jquery

我的代码

success: function(result) {
    var htmlString = '';
    htmlString+= '<div class=\"row mgt45 bg-delete\"><div class=\"col-sm-5\"><div class=\"p-relative\"><img id=\"logoImg\" src=\"result.image\" width=\"100%\"><button id=\"result.id\" class=\"change-img topleft\"   data-toggle=\"modal\" data-target=\"#myModal\"><i class=\"fa fa-camera\" aria-hidden=\"true\"></i> 画像</button></div> </div><div class=\"col-sm-7 corp-office\"><h2 id=\"title_result.id\" class=\"changeableFacility\" contenteditable=\"true\">result.title</h2><p id=\"content1_result.id\" class=\"changeableFacility\" contenteditable=\"true\">result.content1</p><p id=\"result.content2\" class=\"changeableFacility\" contenteditable=\"true\">result.content2</p></div><span id=\"delete_result.id\" href=\"\" class=\"btn-delete\"><i class=\"fa fa-times-circle\" aria-hidden=\"true\"></i></span> </div>';

    $('#addItem').before(htmlString);
},

如何将result.idresult.image存储为变量而不是字符串?

2 个答案:

答案 0 :(得分:2)

使用ECMAScript 2015&#39; s Template String Literals

var id = 5;
var image = "myimage.jpg";
alert(` <div id=\"${id}\">`);
alert(` <img src=\"${image}\">`)

答案 1 :(得分:0)

你现在的变量只是一个字符串。要使变量的值显示在字符串中,您必须将字符串切成片段并使用加法运算符添加变量,如此。

// ... is the ES6 spread operator
function run() { 
  $.when(...requests).then(() => {
    render(database);
  });
}