gpt4 book ai didi

javascript - Mousedown 和 Mouseup 与触发调用有关?

转载 作者:行者123 更新时间:2023-12-01 01:21:16 25 4
gpt4 key购买 nike

In my code i am using two button to display different Display from the same page as two color. When i click on green color for 3 second then only i am firing the call and displaying related data. Red button functions as a normal function and it works.

我的问题是第一次单击“尝试绿色”按钮 3 秒,它没有任何反应,我必须第二次单击 3 秒才能显示绿色。点击“TRY IT GREEN”按钮两次后,以后也可以一键使用。

Any help would be highly appreciated, so that i dont have to click two times at the beginning.

   
var pressme = document.getElementById('pressme');
var button1=document.getElementById('myBtn');
var button2=document.getElementById('myBtn1');

var showme = document.getElementById('showme');
button1.addEventListener('click', display1

);
button2.addEventListener('click', display2

);
function display1(){ //displaying red background
pressme.style.display = 'none';
showme.style.display = 'block';
button1.style.display= 'none';
button2.style.display= 'block';



}

function display2(){ //displaying green background after 3 sec


let timeout = null;
button2.addEventListener( 'mousedown', event => {
timeout = setTimeout(() => {
pressme.style.display = 'block';
showme.style.display = 'none';
button2.style.display= 'none';
button1.style.display= 'block';

}, 3000 );
});
button2.addEventListener( 'mouseup', event => {

if ( timeout ) clearTimeout( timeout );
});

}
<!DOCTYPE html>
<html>
<head>
<title>Realtime communication with WebRTC</title>
<link rel="stylesheet" href="css/main.css" />
</head>
<body>


<table id="pressme" style="width:100%" bgcolor="red" height =500px>
<button id="myBtn">Try it red</button>

</table>


<table id="showme" style="width:100%;display:none;" bgcolor="green"height=500px>
<button id="myBtn1" style=display:none; >Try it green </button>

</table>

</body>
</html>

最佳答案

您之前需要附加事件监听器,问题是您在单击按钮后附加了鼠标事件监听器,因此它们第一次不起作用。

工作代码:

var pressme = document.getElementById('pressme');
var button1=document.getElementById('myBtn');
var button2=document.getElementById('myBtn1');

var showme = document.getElementById('showme');

button1.addEventListener('click', display1);

function display1(){ //displaying red background
pressme.style.display = 'none';
showme.style.display = 'block';
button1.style.display= 'none';
button2.style.display= 'block';
}


button2.addEventListener( 'mousedown', event => {
timeout = setTimeout(() => {
pressme.style.display = 'block';
showme.style.display = 'none';
button2.style.display= 'none';
button1.style.display= 'block';
}, 3000 );
});

button2.addEventListener( 'mouseup', event => {
if ( timeout ) clearTimeout( timeout );
});
<!DOCTYPE html>
<html>
<head>
<title>Realtime communication with WebRTC</title>
<link rel="stylesheet" href="css/main.css" />
</head>
<body>


<table id="pressme" style="width:100%" bgcolor="red" height =500px>
<button id="myBtn">Try it red</button>

</table>


<table id="showme" style="width:100%;display:none;" bgcolor="green"height=500px>
<button id="myBtn1" style=display:none; >Try it green </button>

</table>

</body>
</html>

关于javascript - Mousedown 和 Mouseup 与触发调用有关?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54215407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com