简体   繁体   English

为什么Jquery在img src前面加上窗口URL?

[英]Why is Jquery prepending img src with window url?

So I have this simple code: 所以我有这个简单的代码:

var openPopUp = function(_type, _src) {
    $(_type).bind('click', function(e) {
        e.preventDefault();
        $('#img_pop_up').bPopup({
            onOpen: function() {
                console.log(document.getElementById("jpg_pop_up")['src']);
                $("#jpg_pop_up").attr("src",_src);
                /*$("#jpg_pop_up").attr("src","http://freefever.com/stock/2013-abstract-hd-wallpaper.jpg");*/
            }
        });
    });
}

The line mentioned in the comment works just fine but when I pass a variable as _src it prepends the src with window.location url and then tries to get the image. 注释中提到的那行效果很好,但是当我将变量传递为_src时,它会在src之前添加window.location url,然后尝试获取图像。 Its like http://myserver.com/http://image_url/ Any idea why is that? 就像http://myserver.com/http://image_url/一样,为什么? I am passing the absolute url. 我正在传递绝对网址。 When I do console.log(_src) it prints the complete url fine but when I print 当我执行console.log(_src)时,它会打印完整的URL,但是当我打印时

console.log(document.getElementById("jpg_pop_up")['src']);

this prints my window location url! 这会打印我的窗口位置网址! It is supposed to be null. 应该为空。

When I hardcode the path of the url it works fine but when I pass the path with a variable then it assumes that as a relative path. 当我对网址的路径进行硬编码时,它可以正常工作,但是当我使用变量传递路径时,它会假定该路径为相对路径。

Try this: 尝试这个:

console.log(document.getElementById('jpg_pop_up').getAttribute("src"));

The important difference here is that the src attribute contains what you gave it, but the src property contains the fully-resolved URL. 此处的重要区别在于src 属性包含您提供的内容,而src 属性包含完全解析的URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM