javascript - how to change td color, by click on the td(like battleship game)? - Stack Overflow

how to change td color, by click on the td(like battleship game)? here is the code'with specific

how to change td color, by click on the td(like battleship game)? here is the code'with specific id :

str +="<td id='" + counter + "' onclick='checkClick(" + counter + "," + i + "," + j + ")' style = 'background-color: Black; height:30px; width:30px'>";

(the str in the end will be use with innerHTML that will update this on div inside the body

the checkClick function need to change it , i tried with innerHTML or to get the td by ElementID and it didnt work

how to change td color, by click on the td(like battleship game)? here is the code'with specific id :

str +="<td id='" + counter + "' onclick='checkClick(" + counter + "," + i + "," + j + ")' style = 'background-color: Black; height:30px; width:30px'>";

(the str in the end will be use with innerHTML that will update this on div inside the body

the checkClick function need to change it , i tried with innerHTML or to get the td by ElementID and it didnt work

Share Improve this question edited Jun 17, 2012 at 21:48 ThiefMaster 319k85 gold badges607 silver badges646 bronze badges asked Jun 17, 2012 at 21:45 Mia WMia W 493 silver badges11 bronze badges 2
  • 1 Definitely have a look at quirksmode/js/introevents.html. A prehensive guide about event handling in JS. – Felix Kling Commented Jun 17, 2012 at 21:52
  • My suggestion would be not to += the innerHTML I would append() as I found out in my own question – gabeio Commented Jun 17, 2012 at 22:03
Add a ment  | 

2 Answers 2

Reset to default 3
# my_markup.html
<td id="clickable">Blah</td>

# my_scripts.js
document.getElementById("clickable").addEventListener("click", tableClicked);
function tableClicked(e) {
  e.target.style.backgroundColor = '#FF0000';
}

This should do the job:

Changing Background and Text Color of HTML Elements

or this, even simplier approach:

Change the Background Color of an HTML Element (DIV)

<div id="main" class="main">
  <p>hello!</p>
</div>

  <script type="text/javascript">
  function changecolor()
  {
    var mainContent = "";
    mainContent = document.getElementById("main");
    mainContent.style.backgroundColor = "#FFFF99";
  }
  </script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745500102a4630373.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信