我不知道如何才能做到这一点,请提供一些帮助。 我得到一些 json 对象,其中一个值有 html 标签,一些图像,当然我无法获取图像,因为它们有相对的 url,并且它所在的站点位于子域中。 有什么办法可以添加 http://example.com/到已经存在的 images/image.png
谢谢
这是我的 json 响应:
description": "<img src=\"images/stories/Icons/Vacuum.png\" mce_src=\"images/stories/Icons/Vacuum.png\" border=\"0\" alt=\"Vacuum\" title=\"Vacuum\"> <img src=\"images/stories/Icons/Washing_Machine.png\" mce_src=\"images/stories/Icons/Washing_Machine.png\" border=\"0\" alt=\"Washing Machine\" title=\"Washing Machine\"></p>"
for(var i = 0; i < objJsonA.length; i++){
var ap = objJsonA[i];
var lot = ap.lot;
$('.myclass').html(lot);
}
这会返回 404 错误,我获取的内容位于 www.mysite.com 上,而我位于 m.mysite.com 上
最佳答案
您可以使用document.location
来获取当前服务器。例如,在 Chrome 控制台中该服务器的 URL 上,我看到:
> document.location
Location
ancestorOrigins: DOMStringList
assign: function () { [native code] }
hash: ""
host: "stackoverflow.com"
hostname: "stackoverflow.com"
href: "http://stackoverflow.com/questions/13754450/adding-extra-parameters-to-url-src-with-jquery"
origin: "http://stackoverflow.com"
pathname: "/questions/13754450/adding-extra-parameters-to-url-src-with-jquery"
port: ""
protocol: "http:"
reload: function () { [native code] }
replace: function () { [native code] }
search: ""
toString: function toString() { [native code] }
valueOf: function valueOf() { [native code] }
__proto__: Location
所以你可以使用:
var image = '/images/abc.jpg';
var url = document.location.origin + image;
// url is now 'http://stackoverflow.com/images/abc.jpg'
有很多用于在 JavaScript/jQuery 中处理 URL 的插件,使用其中一个可能是有意义的 ( example search )。
关于javascript - 使用jquery向url src添加额外的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13754450/