gpt4 book ai didi

javascript - 如何使用 onmouseover 更改颜色?

转载 作者:行者123 更新时间:2023-11-30 09:54:01 27 4
gpt4 key购买 nike

我想知道如何将这 2 个函数合并为 1 个函数。

 function test1() {
document.getElementById('div1').style.color = 'red';
}
function test2() {
document.getElementById('div1').style.color = 'blue';
}


<div onmouseover="test1()" onmouseout ="test2()" id="div1">
</div>

最佳答案

将对象和颜色传递给test 方法

 function test(thisObj, color) 
{
thisObj.style.color = color;
}

<div onmouseover="test(this, 'red')" onmouseout ="test(this, 'green')" id="div1"></div>

关于javascript - 如何使用 onmouseover 更改颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34939857/

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