0

私はこれをどのように行うことができるかについて考えることができません。いくつかのjsonオブジェクトを取得し、値の1つに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にあります。

4

1 に答える 1

2

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を操作するためのプラグインはたくさんあり、おそらくそのうちの1つを使用するのが理にかなっています(検索例)。

于 2012-12-06T23:29:18.953 に答える