gpt4 book ai didi

javascript - 外部 单击以重置颜色?

转载 作者:行者123 更新时间:2023-11-29 16:32:41 28 4
gpt4 key购买 nike

我正在尝试将颜色背景更改为默认背景颜色,当我点击输入框外

我尝试使用“null”或“none”但没有用?

window.addEventListener('click', outsideClick);

function changeBackground(x){
var body = document.querySelector('body');
body.style.backgroundColor = x.value;
}
function outsideClick(e) {
if(e.target == backgroundColor) {
body.style.backgroundColor = 'none';
}
}
</head>
<body>
<div class="form-group col-md-4">
<label for="inputState">Change Color</label>
<select id="inputState" class="form-control" onchange="changeBackground(this)">
<option value="red">Red</option>
<option value="yellow">Yellow</option>
</select>

最佳答案

您需要更改自:

if(e.target == backgroundColor) {

到:

if(e.target.tagName != 'SELECT') {

window.addEventListener('click', outsideClick);

function changeBackground(x){
var body = document.querySelector('body');
body.style.backgroundColor = x.value;
}
function outsideClick(e) {
if(e.target.tagName != 'SELECT') {
document.body.style.backgroundColor = '';
}
}
<div class="form-group col-md-4">
<label for="inputState">Change Color</label>
<select id="inputState" class="form-control" onchange="changeBackground(this)">
<option selected>Choose...</option>
<option value="red">Red</option>
<option value="yellow">Yellow</option>
</select>
</div>

关于javascript - 外部 单击以重置颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54382897/

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