在JavaScript中的onclick事件(onclick event in the javascript)

编程入门 行业动态 更新时间:2024-10-25 06:26:56
在JavaScript中的onclick事件(onclick event in the javascript)

我很新的JavaScript我知道你可以添加一个onclick =“”事件到一个html元素......但它可能在JavaScript本身声明,当有人点击x元素事件触发?

I am very new to javascript I know that you can add an onclick="" event to a html element... but is it possible in the javascript itself to declare that when someone clicks on x element an event is triggered?

最满意答案

<input id="myElement" type="button" value="Click me!" /> <script type="text/javascript"> document.getElementById('myElement').onclick = function () { alert('Hello, world!'); } </script>

确保在元素已经存在(底部的脚本)或者DOM准备好之后运行它。 (你可以使用window.onload ,但是你可能只想从一开始就使用jQuery,这样你就可以获得一个神奇的DOM就绪函数, onload有一些缺点,比如等待图像加载。)

<input id="myElement" type="button" value="Click me!" /> <script type="text/javascript"> document.getElementById('myElement').onclick = function () { alert('Hello, world!'); } </script>

Make sure that you either run this after the element already exists (scripts at the bottom), or when the DOM is ready. (You could use window.onload for that, but you might just want to use jQuery from the beginning so that, among other things, you get a magical DOM-ready function. onload has some downsides, like waiting for images to load.)

更多推荐

onclick,html,事件,javascript,电脑培训,计算机培训,IT培训"/> <meta name="de

本文发布于:2023-08-07 20:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465620.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:事件   onclick   JavaScript   event   javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!