简体   繁体   English

当我单击按钮时标题不会改变怎么办?

[英]How come when i click the button my heading doesnt change?

I know this should be fairly easy, and for some odd reason of all functions work except for this one. 我知道这应该很容易,并且出于某种奇怪的原因,除了这一功能之外,所有功能都可以正常工作。 I don't see anything I could've possibly done wrong to prevent the onClick event to change the header to blue. 我看不到我可能做错了什么,以防止onClick事件将标题更改为蓝色。 Can someone please tell me what I'm doing wrong? 有人可以告诉我我在做什么错吗?

Here is the jfiddle: https://jsfiddle.net/CheckLife/680dvv5j/13/ 这是jfiddle: https ://jsfiddle.net/CheckLife/680dvv5j/13/

 #change { color: red; } 
 <h1 id="change">NBA Legends</h1> <button onclick="changedColor()">About</button> <script> document.getElementById('change').onclick = changeColor; function changeColor() { document.body.style.color = "blue"; return false; } </script> 

If you apply the style to body, it will be overriden because the element has it's own style. 如果将样式应用于正文,则它将被overriden因为element具有其自己的样式。

So , add the style to the element. 因此,将样式添加到元素。

change document.body.style.color to document.getElementById('change').style.color document.body.style.color更改为document.getElementById('change').style.color

See below 见下文

 document.getElementById('change').onclick = changeColor; function changeColor() { document.getElementById('change').style.color = "#00f"; return false; } 
 #change { color:red; } 
 <h1 id="change">NBA Legends</h1> <button onclick="changeColor()">About</button> 

you have a typo is 你有错字是

<button onclick="changeColor()">About</button>

instead of 代替

<button onclick="changedColor()">About</button>

Change button onclick Function name changedColor to changeColor . 更改button的onclick功能名称changedColorchangeColor

  document.getElementById('change').onclick = changeColor; function changeColor() { var element = document.getElementById('change') element.style.color = "blue"; return false; } 
 #change { color:red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <h1 id="change">NBA Legends</h1> <button onclick="changeColor()">About</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM