【问题标题】:Forward Slashes ('/') Are Not Getting Created While Appending - jQuery追加时未创建正斜杠('/') - jQuery
【发布时间】:2016-01-19 23:00:42
【问题描述】:

在 ajax 返回成功时,在成功状态下,我运行此附加:

$('hello').append('
 <div class="row" style="background-image: url("/page/12/image-' + user[i]['id'] + '"); height: 155px;" ></div>')

我还有几行,一切正常。然而,在这条线上,由于某种原因,这条线在我的view:source 中被创建为:

<div class="row" style="background-image: url(" profile page 12 image 1.png") height: 155px"></div>

并且图像没有被创建,因为它在附加时删除了正斜杠('/')。

【问题讨论】:

    标签: javascript jquery html append


    【解决方案1】:

    你刚刚把引号嵌套错了,忘记转义最里面的单引号。

    我逐步完成了你的示例并让它工作

    HTML:

    <div class="hello"></div>
    

    Js(包括 jQuery 1.8.3):

    $('.hello').append('<div class="row" style="background-image: url(\'/page/12/image-' + 5 + '\'); height: 155px;"></div>');
    

    【讨论】:

      【解决方案2】:

      这样就可以了:

      $('hello').append('<div class="row" style="background-image: url(\'/page/12/image-' + user[i]['id'] + '\'); height: 155px;" ></div>')
      

      【讨论】:

        【解决方案3】:

        问题似乎是在 string 中混合了双引号和单引号。尝试在hello 之前添加id #class . 选择器

          var user = {
              0: {
                id: "cats"
              }
            },
            i = 0;
        
          var elem = $("<div />", {
            "class": "row",
            "css": {
              "backgroundImage": "url(http://lorempixel.com/155/155/" + user[i].id + ")",
              "height": "155px"
            }
          });
        
          $(".hello").append(elem);
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
        </script>
        <div class="hello"></div>

        【讨论】:

          猜你喜欢
          • 2011-04-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-11-14
          相关资源
          最近更新 更多