简体   繁体   English

Javascript-变量中的双引号

[英]Javascript - double quotes in variable

I am trying to create html tags dynamically using javascript. 我正在尝试使用javascript动态创建html标签。 Please refer to below code. 请参考下面的代码。

function getPhotosSuccess(data) {

    if (data.d.results.length > 0) {

        var response = data.d.results;
        var innerhtml = "";

        $.each(response, function(key, value) {
            var newItem = '<div class="item"><div class="well"><a href="' + value.ServerRelativeUrl + '" data-lightbox="roadtrip" data-title="' + value.ListItemAllFields.Image_x0020_Description + '"><img class="img-responsive" src="' + value.ServerRelativeUrl + '" alt=""></a></div><div><p>' + value.ListItemAllFields.Image_x0020_Description + '</p></div></div>';
            innerhtml += newItem;
        });

        $("#masonryPicGallery .row:eq(0)").html(innerhtml);

    } else {

        console.log("empty response from server"); 
    }
}

My problem is, here variable value.ListItemAllFields.Image_x0020_Description contains a string that starts with double quote. 我的问题是,这里的变量value.ListItemAllFields.Image_x0020_Description包含一个以双引号开头的字符串。

So my html renders as following --> 所以我的html呈现如下->

在此处输入图片说明

Can anybody please help me out with this scenario. 有人可以在这种情况下帮助我吗?

If you're using jQuery (and the presence of Lightbox attributes in your snippet suggests you are) you can use that to avoid using unreliable string concatenation to build your elements: 如果您使用的是jQuery(并且您的代码段中暗示有Lightbox属性),则可以使用它来避免使用不可靠的字符串串联来构建元素:

// create DOM entries without variables
var $newItem = $('<div class="item"><div class="well"><a data-lightbox="roadtrip"><img class="img-responsive" alt=""></a></div><div><p></p></div></div>');

// then populate the appropriate fields using jQuery DOM traversal
$newItem.find('a').attr({
     'href': value.serverRelativeUrl,
     'data-title': value.ListItemAllFields.Image_x0020_Description
   }).find('img')
     .attr('src', value.ServerRelativeUrl);

$newItem.find('p').text(value.ListItemAllFields.Image_x0020_Description);

I believe you should just have to remove the double quotes that come just before the single quotes since those are included in the variable anyway. 我相信您应该只删除单引号之前的双引号,因为无论如何它们都包含在变量中。 So like this: 像这样:

var newItem = '<div class="item"><div class="well"><a href="' + value.ServerRelativeUrl + '" data-lightbox="roadtrip" data-title=' + value.ListItemAllFields.Image_x0020_Description + '><img class="img-responsive" src="' + value.ServerRelativeUrl + '" alt=""></a></div><div><p>' + value.ListItemAllFields.Image_x0020_Description + '</p></div></div>';

If you're able to use ES6 or Babel (which transpiles ES6 into older JavaScript for older browswers) you can also use string templates, which use backticks (left of the '1' key) instead of quotes: 如果您能够使用ES6或Babel(将ES6转换为较旧的浏览器的较旧的JavaScript),则还可以使用字符串模板,该模板使用反引号(“ 1”键的左侧)代替引号:

var newItem = `<div class="item"><div class="well"><a href="${value.ServerRelativeUrl}" data-lightbox="roadtrip" data-title="${value.ListItemAllFields.Image_x0020_Description}"><img class="img-responsive" src="${value.ServerRelativeUrl}" alt=""></a></div><div><p>${value.ListItemAllFields.Image_x0020_Description}</p></div></div>`;

Try to encode the variable quotes: 尝试编码变量引号:

var variable = '"my test value" haha';
variable = variable.replace(/"/g, "&quot;");
console.log(variable);

Here's a snippet with encoding: 这是带有编码的代码段:

 <div class="&quot;test&quot;test">inspect this class</div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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