为什么,在我的代码中更改默认颜色而不是我设置的颜色。
<div>
<p id="demo" name="demo" onmouseover="this.style='green'">Seine Name ist;</p>
</div>
<hr>
<button id="buty" name="buty" onclick="colory();">Click</button>
<script>
function colory(){`enter code here`
document.getElementById('demo').style.color='red';
}
</script>
将this.style
更改为this.style.color
。您也可以使用onmouseout
将颜色更改回“黑色”或“红色”
function colory() {
`enter code here`
document.getElementById('demo').style.color = 'red';
}
<div>
<p id="demo" name="demo" onmouseover="this.style.color='green'">Seine Name ist;</p>
</div>
<hr>
<button id="buty" name="buty" onclick="colory();">Click</button>
感谢您的快速回答,但是现在还有另一个问题,那就是;当我将鼠标移动到“文本”附近或周围时,它会将颜色更改为绿色,我的问题是,为什么不仅仅在文本上(这是onmouseover的功能)?