javascript - 如何避免jquery删除斜线?

标签 javascript jquery html json

我通过 jquery getJSON 获取 url 图像,但随后 jquery 删除斜杠:

//jquery remove slashes:
$("#imgjson").append('<img src"' + field.images[0] + '" /> ');
//here the slashes are not removed:
alert(field.images[0]);

我得到这样的东西:

<img src"http:="" cdn.google.biz="" 34="" test.jpg"="">

而不是这个:

<img src"http://cdn.google.biz/34/test.jpg" />

如何避免这种情况?

最佳答案

问题不在于斜杠。您需要添加=对于 src属性:

$("#imgjson").append('<img src="' + field.images[0] + '" /> ');
//                            ^  

否则,值为 field.images[0]不会被视为 src属性值。它将被视为 <img> 的其他属性元素。

演示

var field = {
  images: [
    "https://media4.giphy.com/media/1UYGwFDvZvnmo/200_s.gif"
  ]
};

$("#imgjson").append('<img src="' + field.images[0] + '" /> ');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<div id="imgjson"></div>

关于javascript - 如何避免jquery删除斜线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31580961/

相关文章:

javascript - 将图像放在 google maps API 标记中的图像上

javascript - 如何在 Controller 中使用 multer 功能而不是在 route 使用

php - 如何在 php 中比较 html 复选框值?

javascript - 网站自动获取消息到 php 聊天应用程序不工作

javascript - Backbone.js 从事件处理程序调用函数渲染

javascript - 使用AJAX数据源时,可以在Select2中设置查询字符串参数吗?

jquery - 为什么使用observe_field代码不能与JQuery模态对话框一起使用?

Jquery无法捕获<form>提交事件

javascript - 动态加载 CSS 样式

javascript - 将 iframe 定位在页 footer 分上方。简单的