gpt4 book ai didi

javascript - 如何让这段JavaScript代码更实用呢?

转载 作者:行者123 更新时间:2023-11-29 18:21:57 26 4
gpt4 key购买 nike

我创建代码作为练习。这会根据您在文本框中键入的内容更改浏览器的背景颜色。除了使用 setInterval() 方法之外,我想不出还有什么其他方法可以做到这一点,但这让我很烦。我觉得必须有更好的方法来做到这一点,它不涉及每十毫秒检查一次更改。任何解决方案?

setInterval(function(){colorNow()},10);
function colorNow(){
var chooseColor = document.getElementById("color").value;;
document.bgColor = chooseColor;
}

最佳答案

我会像这样使用 keyup 事件

var chooseColor = document.getElementById("color");
chooseColor.addEventListener('keyup', function() {
document.bgColor = chooseColor.value;
}, false);

这里有一些关于事件的信息 https://developer.mozilla.org/en-US/docs/Web/API/Event

The keyup event - basically the function provided to the keyup event will be called every time the user releases a key while the input is focused that's because we added the event directly to thechooseColor element

The false section of the function is default (it's called /bubbling|bubble|bubbles/ ) and it stops the event bubbling up the DOM Tree, so if it were true the parent(s) of that element will be notified of the event. https://developer.mozilla.org/en-US/docs/Web/API/event.bubbles

Demo

关于javascript - 如何让这段JavaScript代码更实用呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17756952/

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