在 JavaScript 中的 HTML 图像标签上添加 Onclick 事件
onclick
被添加到 HTML 中的图像标签上。onclick
事件将使我们的图像可点击。用户单击图像后,你可以执行任何操作,例如打开新网页、添加动画、使用新图像更改现有图像等。在 onclick
中,你可以传递一个函数。在这里,你可以创建和调用你用 JavaScript 编写的函数,也可以使用窗口对象(如 window.open()
)提供的现有函数。
本文将展示当用户使用 onclick
事件单击图像时,如何在新的浏览器标签页中显示图像。我们将通过创建一个函数并在 HTML 图像标签的 oncreate
属性中调用它来实现这一点。
为了在 JavaScript 中实现 onclick
事件功能,我们首先必须创建一个函数,然后在 onclick
中调用该函数,该函数出现在 HTML 中的图像标签上。在这里,我们拍摄了一张图片,当用户点击这张图片时,该图片将在一个新的浏览器标签页中打开。我们将在下面的示例中实现这一点。
下面我们有一个基本的 HTML 文档,其中只有一个 img
标签,其中 src
属性设置为从服务器获取的图像。在图像的 onclick
属性上,我们传递 openImg()
函数,该函数将调用该函数。我们还没有创建这个函数。在文件的末尾,我们链接了 JavaScript 文件以创建我们的函数。请参阅下面的代码。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<!-- Image taken from Unsplash -->
<img id="image" src="https://bit.ly/3jFwe3d" onclick="openImg()">
<script src="./script.js"></script>
</body>
</html>
如果你运行上面的代码,这就是它在 Web 浏览器中的样子。
在我们的 JavaScript 文件中,我们定义了一个函数 openImg()
。在这个函数中,我们首先必须获得对 img
标签的引用,该标签使用其 id
属性 image
存在于 DOM 中。这可以使用 document.getElementById()
方法来完成。然后,我们将图像标签的引用存储在 image
变量中。
由于我们想在新的浏览器标签页中显示相同的图像,我们还必须将图像源存储在一个变量中,我们可以使用 src
属性获取该变量。在 JavaScript 中,我们只需要使用 image.src
来访问 source 属性,然后我们将它存储在 source
变量中。
最后,要使用其源显示图像,我们可以使用 window.open()
方法。window.open()
方法用于打开一个新标签页,我们在此函数中传递的任何内容都将显示在新标签页中。在这里,我们将传递源变量,其中包含图像本身的链接。这就是我们的 JavaScript openImg()
函数的样子。
function openImg(){
var image = document.getElementById('image');
var source = image.src;
window.open(source);
}
如果你在 Web 浏览器中运行上述代码并单击图像,则图像将在新的浏览器标签页中打开,如下所示。
HTML onclick
图像属性有助于使图像在 JavaScript 中可点击。这可以在各种情况下使用,具体取决于你希望为网站实施的功能类型。
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
用 jQuery 检查复选框是否被选中
发布时间:2024/03/24 浏览次数:102 分类:JavaScript
-
在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的
jQuery 中的 Window.onload 与 $(document).ready
发布时间:2024/03/24 浏览次数:180 分类:JavaScript
-
本教程演示了如何在 jQuery 中使用 Window.onload 和 $(document).ready 事件。