开发者

Selected List Item Color Change

开发者 https://www.devze.com 2023-03-22 16:23 出处:网络
I am creating a Web page that contains the following code. <script> function changeColor(c) { document.getElementById(\"message\").style.color=c;

I am creating a Web page that contains the following code.

<script>
function changeColor(c) {
document.getElementById("message").style.color=c;
}
</script>
<p id="message">Welcome!</p>
<ul id="color">
<li>Black</li>
<li>Red<开发者_运维技巧;/li>
</ul>

I need to ensure that when I clicks an item in the list, the text color of the Welcome! message will change. Which declaration should you use?


<li onclick="changeColor(this.innnerHtml)"> Black </li>


New: JS Fiddle using the font color (not text) and JS to attach the event


JS Fiddle of the Below

<script>
   function changeColor(c) {
      document.getElementById("message").style.color=c;
   }
</script>

<p id="message">Welcome!</p>
<ul id="color">
   <li onclick="changeColor(this.innerHTML);">Black</li>
   <li onclick="changeColor(this.innerHTML);">Red</li>
</ul>

Note:

  1. This is not the best way to do this, especially if you have many list elements. A better way would be to use JavaScript; get the color unordered list, loop through each loop element, and add the changeColor(this.innerHTML) to the click event.

  2. innerText may be used instead of innerHTML

0

精彩评论

暂无评论...
验证码 换一张
取 消