【问题标题】:Concatenation with jquery与 jquery 连接
【发布时间】:2013-02-25 17:59:26
【问题描述】:

不确定如何以正确的方式编写此内容。我可以让它工作,但是当我查看源代码时,它看起来像这样

<img "="" "style="width:30%; height:30%;"   src="correctsource"></img>

正如您在图像标签的开头看到的那样,它们是两个额外的引号和一个等号。

这是我的代码

var img = $(this).attr('src');
  //grab the visible div and the div with class edititable within it and append image
  $(".open:visible").find('.edititable').append('<img src="' + img + '" "style="width:30%; height:30%;" ">');

【问题讨论】:

  • img 标签末尾的额外引号

标签: javascript jquery string concatenation


【解决方案1】:

改为:

foo.append(
    $("<img />", { 
        src: img, 
        style: "width: 30%; height: 30%" 
    })
);

这样更容易避免引号不匹配的错误,并且通常也提高了可读性

【讨论】:

【解决方案2】:

关闭,您的字符串格式不正确。你有一个额外的“风格前面。技术你把它写成

$(".open:visible").find('.edititable').text('<img src="' + img + ' " style="width:30%; height:30%;" ">');

相关小提琴:http://jsfiddle.net/6cLYf/

【讨论】:

    【解决方案3】:

    删除最后一个双引号,然后它会起作用。你添加了一个额外的。

    就是这样写

     .................height:30%;">');
    

    【讨论】:

      【解决方案4】:

      试试这个,你的字符串中的双引号太多了。

      $(".open:visible").find('.edititable').append('<img src="' + img + '" style="width:30%; height:30%;">');
      

      【讨论】:

        猜你喜欢
        • 2014-06-22
        • 1970-01-01
        • 1970-01-01
        • 2017-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多