我想在点击使用javascript后更改背景颜色

时间:2012-02-04 12:18:29

标签: php javascript jquery

以下是我的代码:

<table border="0" align="center" cellpadding="0" cellspacing="0" class="grid_table">
    <tr class="grid_caption"><td class="grid_caption">Customer Name</td></tr>
    <? if(count($arrResult) > 0) { ?>
        <? foreach($arrResult as $row) { ?>
        <tr class="grid_row"  ><td class="grid_row" onClick="javascript:return selectCustomer(this,<?=$row['customer_id']?>);"><?=HTMLVarConv($row['Name'])?></td></tr>
        <? } ?>
    <? } ?>
</table>

这里我得到了客户名单 但是当我点击这个客户名称时,它的背景颜色应该改变.. 这是我的javascript函数

function selectCustomer(row, id) {
    alert(row);
    var mark_color = '#fdf5d2' //'#c1c1ff'
    var current_row = '#aad4f0' //'#e8e8e8'
    if (row.style.backgroundColor == current_row) {
        row.style.backgroundColor = mark_color;
    }
    else if (row.style.backgroundColor == mark_color) {
        row.style.backgroundColor = current_row;
    }
    $("#editCustomer").attr("src","editcustomer.php?customer_id="+id+"&showmenu=0");
    $("#ajaxList").show();
    return;
}

但没有改变什么问题???? 请帮帮我

3 个答案:

答案 0 :(得分:1)

由于您标记了“jquery”,为什么不使用它而不是内联onclick处理程序?它可以很简单:http://jsfiddle.net/t7vj5/

$("td.grid_row").click(function() {
    $(this).toggleClass("selected");

    $("#editCustomer").attr("src", "editcustomer.php");
    $("#ajaxList").show();

    return false;
});

CSS:

td.grid_row {
    background-color: #fdf5d2;
}

td.grid_row.selected {
    background-color: #aad4f0;
}

确保在创建行后执行JavaScript,或将其包装在$(document).ready(function() { ... })内。

答案 1 :(得分:0)

$('.grid_row').click(function(){
$('.grid_row').css("background-color","#aad4f0");
$(this).css("background-color","#fdf5d2");
});

编辑:哦,你可以删除onclick =功能 只要类gridrow在那里,上面的行就足以满足您的需要。

这将在单击时将颜色更改为#fdf5d2,并在单击另一行时将颜色更改回#aad4f0以用于所有其他单元格。 如果您不想更改颜色,请删除第一行

答案 2 :(得分:0)

这个很完美: CSS ::

.grid_row
{
    padding: 3px;
    border-right: 1px solid #849AAA;
    border-bottom: 1px solid #849AAA;
    vertical-align:top;
}
.evn
{
    padding: 3px;
    border-right: 1px solid #849AAA;
    border-bottom: 1px solid #849AAA;
    vertical-align:top;
    background-color: white;
    /*document.body.style.cursor = 'wait';*/
    cursor: pointer;
    }
.selevn
{
    padding: 3px;
    border-right: 1px solid #849AAA;
    border-bottom: 1px solid #849AAA;
    vertical-align:top;
    background-color: silver;
    cursor: pointer;
}

以下是我的Javascript代码: -

<script type="text/javascript">
var prevRow = null;
function selectCustomer(it,id) {
    if (it.className.substring(0, 3) == "sel")
    {
        it.className = it.className.substring(3, 6);
        prevRow = null;
    }
  else
    {
        it.className = "sel" + it.className;
        if (prevRow != null)
        {
           prevRow.className = prevRow.className.substring(3, 6);
        }
        prevRow = it;
    }
    $("#editCustomer").attr("src","editcustomer.php?customer_id="+id+"&showmenu=0");
    $("#ajaxList").show();
    return ;
}

这是我的代码:

<table border="0" align="center" cellpadding="0" cellspacing="0" class="grid_table">
        <tr class="grid_caption"><td class="grid_caption">Customer Name</td></tr>
        <? if(count($arrResult) > 0) { ?>
        <? foreach($arrResult as $row) { ?>
        <tr class="evn"  onClick="javascript:return selectCustomer(this,<?=$row['customer_id']?>);" ><td class="grid_row" id="<?= $row['customer_id']?>"  ><?=HTMLVarConv($row['Name'])?></td></tr>
        <? } ?>
        <? } ?>
        </table>