gpt4 book ai didi

why my function not working correctly on addeventlistener(为什么我的函数在addeventlistener上不能正常工作)

转载 作者:bug小助手 更新时间:2023-10-28 10:31:16 26 4
gpt4 key购买 nike



I just try to show some text on button after user clicking on that button, for example when clicking on btn 'Submit' it changes to 'plz check your...'

我只是试着在用户点击按钮后在按钮上显示一些文本,例如,当点击BTN‘提交’时,它会变成‘请检查您的...’。


My CSS style:

我的css风格:


#my-btn:active
{
visibility: hidden;
}

My JavaScript code:

我的JavaScript代码:


const myTurn = document.querySelector('#my-btn');
myTurn.addEventListener('click', function(e) {
e.preventDefault();
myTurn.style.visibility = 'visible';
myTurn.innerText = 'plz check your...';
});

It does not work with above code but work with calling the whole document.

它不适用于上述代码,但适用于调用整个文档。


document.addEventListener('click', function(e) {
e.preventDefault();
myTurn.style.visibility = 'visible';
myTurn.innerText = 'plz check your...';
});

But the problem with above code is if I click anywhere on the page it would change the button inner text.

但上述代码的问题是,如果我单击页面上的任何位置,它会更改按钮的内部文本。


更多回答

Check the browser console. There may be some errors, otherwise your code looks fine.

检查浏览器控制台。可能有一些错误,否则您的代码看起来没有问题。

优秀答案推荐

Your code works fine. This is your code exactly as you wrote it. I've added a button element with id="my-btn" so it can be selected

您的代码运行良好。这是与您编写的代码一模一样的代码。我添加了id=“my-btn”的按钮元素,这样就可以选择它了




const myTurn = document.querySelector('#my-btn');
myTurn.addEventListener('click', function(e) {
e.preventDefault();
myTurn.style.visibility = 'visible';
myTurn.innerText = 'plz check your...';
});

<button type="button" id="my-btn">My Button</button>




更多回答

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