仅创建第二次单击事件Javascript(Create Second Click Event Javascript Only)

编程入门 行业动态 更新时间:2024-10-14 02:23:41
仅创建第二次单击事件Javascript(Create Second Click Event Javascript Only)

当我单击按钮然后再次单击按钮时更改后,我需要更改图像不透明度。 我只能使用Javascript。 我已经设置了2个if / else语句,当我第一次发生事件时单击按钮但是我无法进行第二次事件。 我是javascript的新手。

var img = document.querySelector('#img'); var button1 = document.querySelector('#button1'); var bool = new Boolean(); if (chrome.style.opacity = "1.0"){ bool = true; } else if (chrome.style.opacity = "0.5"){ bool = false; } if (bool){ button1.addEventListener('click', function() { chrome.style.opacity = "0.5"; }); } else{ button1.addEventListener('click', function() { chrome.style.opacity = "1.0"; }); }

I need to have an image change opacity when I click a button and then change back when I click the button again. I can only use Javascript. I've set up 2 if/else statements and when I click the button the first time the event happens but I can't get a second event to happen. I'm very new to javascript.

var img = document.querySelector('#img'); var button1 = document.querySelector('#button1'); var bool = new Boolean(); if (chrome.style.opacity = "1.0"){ bool = true; } else if (chrome.style.opacity = "0.5"){ bool = false; } if (bool){ button1.addEventListener('click', function() { chrome.style.opacity = "0.5"; }); } else{ button1.addEventListener('click', function() { chrome.style.opacity = "1.0"; }); }

最满意答案

尝试这个

HTML

<button id="button1">hi</button>

CSS

button { opacity: 0.5; }

JS

button1.addEventListener('click', function() { this.style.opacity = (this.style.opacity == 1.0) ? 0.5 : 1.0; });

我想你的js实际上就是这个..只是适应它

button1.addEventListener('click', function() { chrome.style.opacity = (chrome.style.opacity == 1.0) ? 0.5 : 1.0; });

http://jsfiddle.net/Q4C6g/

Try this

html

<button id="button1">hi</button>

css

button { opacity: 0.5; }

js

button1.addEventListener('click', function() { this.style.opacity = (this.style.opacity == 1.0) ? 0.5 : 1.0; });

i suppose your js would actually be this ..just adapt it

button1.addEventListener('click', function() { chrome.style.opacity = (chrome.style.opacity == 1.0) ? 0.5 : 1.0; });

http://jsfiddle.net/Q4C6g/

更多推荐

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

发布评论

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

>www.elefans.com

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