开发者

How, using jQuery, might I obtain the src attribute of a HTML img element?

When the mouse cursor moves over an image,开发者_StackOverflow社区 I would like to display an alert() containing the value of that image's src attribute. How could I go about accomplishing this?


You can use the mouseover event.

If you have

<img src='foo.jpg' id='bar'>

You can have some jQuery code like

$('#bar').mouseover(function(){ alert($(this).attr('src')); });

(if this fails you could also try replacing $(this) with $('#bar'), but as noted in the comments it's pretty ugly)

edit: missed the need to display the src attribute first time through..


JavaScript:

function alertSource( image ) {
   alert( image.src );
}

HTML:

<img src="path/to/image" onmouseover="alertSource(this);" alt=""/>

You do not need jQuery for this.


<img src="some_img.gif">
<script>
$("img").bind("mouseover",function(){
alert($(this).attr("src"));
});
</script>


$('img').mouseover(function() { 
    alert( this.src );
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜