此对象的引用

问题描述 投票:0回答:2

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

<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 javascript-objects
2个回答
0
投票

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>

0
投票

感谢您的快速回答,但是现在还有另一个问题,那就是;当我将鼠标移动到“文本”附近或周围时,它会将颜色更改为绿色,我的问题是,为什么不仅仅在文本上(这是onmouseover的功能)?

© www.soinside.com 2019 - 2024. All rights reserved.