我想通过JavaScript弹出IMG URL,这是IMG标记通过定义的如何提醒()IMG的URL?
我不知道如何调用它。
alert(document.getElementById("my_image").src);
我想通过JavaScript弹出IMG URL,这是IMG标记通过定义的如何提醒()IMG的URL?
我不知道如何调用它。
alert(document.getElementById("my_image").src);
这是更好地使用getAttribute()
功能:
var imageSource = document.getElementById("my_image").getAttribute('src');
alert(imageSource);
使用getAttribute()
更适合您的应用程序比单纯使用element.src
,因为它将返回明确定义值的属性里面,而不是解析值。
看到这里比较>http://jsfiddle.net/9dAjL/
这个代码是罚款
alert(document.getElementById("my_image").src);
我会写更多像这样
var imageSrc = document.getElementById("my_image").src;
alert(imgSrc);
但reguardless,其优良的,我猜你是不是将其连接到一个点击事件或任何事件,这里是onload事件
window.onload = function() {
var imageSrc = document.getElementById("my_image").src;
alert(imgSrc);
}
有不同的方式。试试这些:
<script type="text/javascript">
window.onload = function()
{
alert(document.getElementById("my_image").src);
alert(document.getElementById("my_image").getAttribute('src'));
}
</script>
<img src="http://localhost/test.jpg" id="my_image" />
更具体地讲一下你需要什么。
或者你可以使用名称:
<script type="text/javascript">
window.onload = function()
{
alert(document["ima"].src);
}
</script>
<img src="http://localhost/ima.jpg" name="ima" />
你可以张贴一些HTML? – David
http://www.ehow.com/how_8049962_source-image-element-using-javascript.html – Mooseman
你的问题是什么?你的代码现在不工作吗? – Davit