我很新的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
发布评论