我想从此 a href 返回图像宽度和高度。
<a id="CloudThumb_id_1" class="cloud-zoom-gallery" rel="useZoom: 'zoom1', smallImage: 'http://www.example.com/598441_l2.jpg'" onclick="return theFunction();" href="http://www.example.com/598441_l2.jpg">
我正在使用警报测试 theFunction,它工作正常,但我不太确定如何返回高度和宽度?
function theFunction () {
alert('This is a test' );
最佳答案
对我来说,这是我所做的一个示例,这段代码没什么特别的,只需更改下面图像的 src,该函数就会自动告诉您图像的宽度和高度。我不知道为什么你的函数要返回一个值,但对我来说它完全没有必要。另外,您的 href 链接不起作用
<html>
<head>
<script type="text/javascript">
function theFunction()
{
var width1; var height1;
width1 = document.getElementById("sampleimage").width;
height1 = document.getElementById("sampleimage").height;
document.getElementById("output").innerHTML = "Width = "+width1 + " Height = " + height1;
}
</script>
</head>
<body>
<img src="sample.jpg" id="sampleimage">
<div id="output">Height and Width?</div>
<button type="button" id="samplebutton" onclick="theFunction()">Check</button>
</body>
希望这有帮助:)
关于javascript - 如何使用 href 中的 onclick 事件返回图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24735648/