简体   繁体   English

JavaScript图像src属性返回错误值

[英]Javascript image src attribute returns wrong value

I have a little javascript code attached to a button with onclick to the following code: 我在onclick上将一些javascript代码附加到按钮上,并将其添加到以下代码:

function ondelete () {

    var getDiv = document.getElementById("imgdiv");
    var lb_img = $("#imgdiv").children();
    for (var i = 0; i < lb_img.length; i++) {
        console.log(lb_img[i].src);
    }
}

This returns a single output which is great, this is the corresponding html when opened in chrome with F12 developers tool: 这将返回一个很棒的输出,这是使用F12开发人员工具在chrome中打开时对应的html:

<div id="imgdiv" class="modal-body next">
    <img draggable="false"src="http://(urlfrommywebsite)/A_LANX/get/OTc1/179f42">
</div>

So the code should return that src attribute, but instead it returns 因此,代码应返回该src属性,但返回

http://127.0.0.1/stage/local/admin/galleries/gallery/2

which is my url. 这是我的网址。 On other images it returns the value of another img or this same url, really weird. 在其他图像上,它返回另一个img或同一URL的值,这确实很奇怪。 This all happens inside a lightbox, so the normal code without a lightbox open is 这一切都发生在灯箱内部,因此未打开灯箱的常规代码为

<div id="imgdiv" class="modal-body next"></div>

I'm really stuck here, so a little help is really usefull :) 我真的被困在这里,所以一点帮助真的很有用:)

EDIT: 编辑:

No answer seems to help so far, any recommendations for other lightboxes with maybe a function like getImage() or something simular? 到目前为止,似乎没有任何答案对您有帮助,对其他带有诸如getImage()之类功能或类似功能的灯箱的建议?

ps fancybox doesn't quitte work for me either. ps fancybox也不适合我。

Use jQuery's attr() to get attributes 使用jQuery的attr()获取属性

$("img").attr("src");

Edit the selector accordingly 相应地编辑选择器

http://jsfiddle.net/b3xdgn83/1 http://jsfiddle.net/b3xdgn83/1

You are using this code to get the image url. 您正在使用此代码来获取图像URL。

<div id="imgdiv" class="modal-body next">
<img draggable="false" src="http://wallpoper.com/images/00/31/33/51/black-background_00313351.jpg">

jQuery Code : jQuery代码:

$( document ).ready(function() {
alert($('#imgdiv img').attr('src'));

}); });

Live Demo 现场演示

you can try this: 您可以尝试以下方法:

function ondelete() {

        var lb_img = $("#imgdiv").children();
        for (var i = 0; i < lb_img.length; i++) {
            console.log(lb_img[i].getAttribute("src"));
        }
    }

for more info about attr() and getAttribute() see this post: Difference between jQuery's attr() and getAttribute() 有关attr()和getAttribute()的更多信息,请参见这篇文章: jQuery的attr()和getAttribute()之间的区别

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

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