THIS 指向对象

THIS referent to the Object

为什么会这样,在我的代码中更改默认颜色而不是我设置的颜色。

<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 将颜色改回 'black' 或 'red'

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 函数的文本上方?