最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - i want to change the background color after clicking? - Stack Overflow

programmeradmin4浏览0评论

I want to change the background color after clicking

This is my HTML code

<td class="meun" onclick="getclick();">...</td>

And this is my JavaScript:

function getclick()
{
    this.style.background-color: #EFF2F7;
}

/

i want when click any on in the td in the table to change it's background color

I want to change the background color after clicking

This is my HTML code

<td class="meun" onclick="getclick();">...</td>

And this is my JavaScript:

function getclick()
{
    this.style.background-color: #EFF2F7;
}

http://jsfiddle/3ZmCh/

i want when click any on in the td in the table to change it's background color

Share Improve this question edited Apr 15, 2011 at 15:00 manji 48k5 gold badges97 silver badges104 bronze badges asked Apr 15, 2011 at 14:51 BuffonBuffon 3,7336 gold badges22 silver badges14 bronze badges 2
  • 4 A question is not formed just by ending a statement with a question mark. – BoltClock Commented Apr 15, 2011 at 14:53
  • 2 Apart from some extra question marks (that I removed), it's a legit question (His english is not fluent, but that's not a requirement to post a question here). – manji Commented Apr 15, 2011 at 15:02
Add a ment  | 

5 Answers 5

Reset to default 3

http://jsfiddle/ThiefMaster/3ZmCh/16/

You need to pass this as an argument and use proper JavaScript in the handler method:

function getclick(elem) {
    elem.style.backgroundColor = '#EFF2F7';
}

Your getclick() function should look like this:

function getclick(el)
{
    el.style.backgroundColor='#888888';
}

In js, styles look a little different

and in your td, do

<td onclick="getclick(this)">

Here's the code in jQuery:

$("td").click(function() {
        $(this).css("background-color","#ABCDEF");
    });
<script language="JavaScript">
<!--
function changeBGC(color){
document.bgColor = color;
}
//-->
</script>

<a href="#" onClick="javascript:changeBGC('#000099')">Click Blue</a>

I would add the id to your <td> tag and change the name of function for a better understanding.

function changeBgColor() {

var tableData = document.getElementById('meun');

tableData.style.backgroundColor = '#EFF2F7';

}




<td id='meun' class="meun" onclick="changeBgColor()">...</td>
发布评论

评论列表(0)

  1. 暂无评论