Javascript 在jQuery中获取IMG标记的ID
在本例中,我为两个图像编写了以下代码-但实际上,它可以复制N次:Javascript 在jQuery中获取IMG标记的ID,javascript,jquery,mouseevent,html,Javascript,Jquery,Mouseevent,Html,在本例中,我为两个图像编写了以下代码-但实际上,它可以复制N次: <figure title="" class="DragBox" dragableBox="true" style="opacity: 1;" id="thumb_1"> <span class="pic" id='UAJ754'> <span class="imagewrap"> </span> <span class="overl
<figure title="" class="DragBox" dragableBox="true" style="opacity: 1;" id="thumb_1">
<span class="pic" id='UAJ754'>
<span class="imagewrap"> </span>
<span class="overlay">
<a href="fulldata.php?UAJ754" class="popUpSimple" rel="thumb_1">Image Information</a>
</span>
<img src="/pics/UAJ754.jpg" alt="">
</span>
<figcaption class="AlbumFig_Caption">A picture caption.
</figcaption>
</figure>
<figure title="" class="DragBox" dragableBox="true" style="opacity: 1;" id="thumb_2">
<span class="pic" id='JB6732'>
<span class="imagewrap"> </span>
<span class="overlay">
<a href="fulldata.php?JB6732" class="popUpSimple" rel="thumb_2">Image Information</a>
</span>
<img src="/pics/JB6732.jpg" alt="">
</span>
<figcaption class="AlbumFig_Caption">A second picture caption.
</figcaption>
</figure>
同样,这很好,但无论单击哪个“pic”span,它都只会给我第一个“pic”span的ID。如何获取当前“pic”跨度的ID字段。。。当用户按下按钮时鼠标在上面的按钮???尝试:
var imgref = $(this).attr('id');
或:
而不是:
var imgref = $(".pic").attr('id');
$.pic将给出第一个类为pic的div,而不是您单击的div
var imgref = $(this).attr('id');
或:
而不是:
var imgref = $(".pic").attr('id');
$.pic将给出具有类pic的第一个div,而不是您单击的类,而不是使用var imgref=$.pic.attr'id'
使用这个:var imgref=$this.attr'id' 而不是使用var imgref=$.pic.attr'id'
使用这个:var imgref=$this.attr'id'
因为这也是一个对象,$这是在另一个对象中包装一个对象
var imgref = $('img', this).attr('src');
因为这也是一个对象,$这是在另一个对象中包装一个对象
var imgref = $('img', this).attr('src');
您将获得IMG的src
您将获得IMG的src。现在使用.prop'id可能更好,或者仅此.id.现在使用.prop'id可能更好,或者仅此.id.可能重复的可能重复的感谢-那么我说$this表示当前对象是对的吗,不管它是什么?谢谢-那么我说$this意味着当前对象,不管它是什么,这是对的吗?