简体   繁体   English

如何将事件侦听器嵌入到 JavaScript 的条件中?

[英]How can I embed an event listener into a conditional in JavaScript?

What is the best syntax to express: "If I click this, this will happen but if it is not clicked, then another action will occur"?表达的最佳语法是什么:“如果我点击它,就会发生这种情况,但如果没有点击它,就会发生另一个动作”? I want to keep CSS, HTML and JS separate, if possible.如果可能的话,我想将 CSS、HTML 和 JS 分开。

I am trying to write code for "If I click this button, then the video will appear & play. But when it is not clicked, the video is hidden".我正在尝试为“如果单击此按钮,则视频将出现并播放。但是当未单击时,视频被隐藏”编写代码。

button = document.getElementByIdId('button')
video = document.getElementById('video')

button.addEventListener('click', function () {
(action here)
}

Add an event listener to your play button where it calls videoElem.play()将事件侦听器添加到调用 videoElem.play() 的播放按钮

HTML: HTML:

<video id='video'>
    <source src="your-video-source.mp4" type="video/mp4">
</video>

<button id='playVid'>Play</button>

Javascript: Javascript:

document.getElementById('playVid').onclick = function () {
    document.getElementById('video').play();
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM