如何使用jQuery可以获取HTML img元素的src属性? [英] How, using jQuery, might I obtain the src attribute of a HTML img element?
本文介绍了如何使用jQuery可以获取HTML img元素的src属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当鼠标光标移动到图像上时,我想显示一个 alert()
,其中包含该图像的 src
属性。如何完成这个?
When the mouse cursor moves over an image, I would like to display an alert()
containing the value of that image's src
attribute. How could I go about accomplishing this?
推荐答案
您可以使用 mouseover event。
You can use the mouseover event.
如果你有
<img src='foo.jpg' id='bar'>
你可以有一些jQuery代码,如
You can have some jQuery code like
$('#bar').mouseover(function(){ alert($(this).attr('src')); });
(如果失败,您还可以尝试替换 $(this) code>与
$('#bar')
,但如评论中所述,它很丑陋)
(if this fails you could also try replacing $(this)
with $('#bar')
, but as noted in the comments it's pretty ugly)
编辑:错过了首次需要显示src属性..
edit: missed the need to display the src attribute first time through..
这篇关于如何使用jQuery可以获取HTML img元素的src属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文