javascript - 如何使用 href 中的 onclick 事件返回图像大小

标签 javascript html

我想从此 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/

相关文章:

javascript - 我已经链接了 jquery 文件,但无法使用它

javascript - 调用父方法(原型(prototype))

php - 图像上传到数据库中未保存在目录中

html - 将表格行分成多行(响应式布局)

javascript - 基于 Node.js + Node-Webkit + Node-SerialPort 的应用程序 - 这可能吗?

javascript - 如何隐藏屏幕外的图像?

javascript - 如何访问reactjs中另一个组件的功能?

javascript - 即使条件变为 false,if 语句中的内容仍保留在 DOM 中 - Handlebars/Ember

javascript - HTML5 History.pushState 破坏包含百分比编码的非 Ascii (Unicode) 字符的 URL

html - MaterializeCss 表单输入(提交)按钮