更改表格的边框和背景颜色并将鼠标悬停在它们上方

时间:2012-05-13 20:42:01

标签: javascript html css

我试图通过单击按钮来更改表格的背景和边框。我也试图通过将鼠标悬停在按钮上来改变颜色。如果我先做的话,我就把悬停上班了。我遇到的问题是,当我单击按钮更改背景颜色时,我无法单击任何其他按钮来更改为该特定按钮颜色。例如,我有四个按钮,蓝色,绿色,黄色,红色。如果我点击蓝色按钮,背景将变为蓝色,然后如果我选择单击其他彩色按钮,它将无法工作,我点击任何按钮一次后我的悬停将不再工作。另外,如何减少耦合。按照我要添加更多颜色按钮的速度,只需要更多的代码行。

 <h1 class="underline">Choose a Background or Border Color</h1>
            <div class="divCenter">
                <div class="divTable" ></div>
            </div></br>
        <button id="button1">Blue</button>
        <button id ="button2">Green</button>
        <button id="button3">Yellow</button>
        <button id ="button4">Red</button></br>
        <button id="button5">Blue Border</button>
        <button id ="button6">Green Border</button>
        <button id="button7">Yellow Border</button>
        <button id ="button8">Red Border</button></br>
        <script>
        $(document).ready(function()
            {
                $("#button1").click(function()
                    {

                        $(".divTable").attr("class","divTableBlue");
                    }); 

                $("#button2").click(function()
                    {
                    $(".divTable").attr("class","divTableGreen");

                    });
                $("#button3").click(function()
                    {
                    $(".divTable").attr("class","divTableBlue");
                    });
                $("#button4").click(function()
                    {
                        $(".divTable").attr("class","divTableRed");
                });
        $("#button1").hover(function()
                    {
                        $(".divTable").addClass("divTableBlue");
                    }, 
                    function() 
                    {
                        $(".divTable").removeClass("divTableBlue"); 
                    });
                $("#button2").hover(function()
                    {
                        $(".divTable").addClass("divTableGreen");
                    }, 
                    function() 
                    {
                        $(".divTable").removeClass("divTableGreen");
                    });
                $("#button3").hover(function()
                    {
                        $(".divTable").addClass("divTableYellow");
                    }, 
                    function() 
                    {
                        $(".divTable").removeClass("divTableYellow");
                    });
                $("#button4").hover(function()
                    {
                        $(".divTable").addClass("divTableRed");
                    }, 
                    function() 
                    {
                        $(".divTable").removeClass("divTableRed");
                    });
            });
        </script>

CSS

.divTable
{
    display:table;
    padding-top:10px;
    padding-bottom:200px;
    padding-left:10px;
    padding-right:10px;
    width:250px;
    background:grey;
    border-style:solid;
    border-width:5px;
}
.divTableBlue
{
    display:table;
    padding-top:10px;
    padding-bottom:200px;
    padding-left:10px;
    padding-right:10px;
    width:250px;
    background:blue;
    border-style:solid;
    border-width:5px;
}
.divTableGreen
{
    padding-top:10px;
    padding-bottom:200px;
    padding-left:10px;
    padding-right:10px;
    width:250px;
    background:green;
    border-style:solid;
    border-width:5px;
}
.divTableYellow
{
    padding-top:10px;
    padding-bottom:200px;
    padding-left:10px;
    padding-right:10px;
    width:250px;
    background:yellow;
    border-style:solid;
    border-width:5px;
}
.divTableRed
{
    padding-top:10px;
    padding-bottom:200px;
    padding-left:10px;
    padding-right:10px;
    width:250px;
    background:red;
    border-style:solid;
    border-width:5px;
}

.divTableBlueBorder
{
    display:table;
    padding-top:10px;
    padding-bottom:200px;
    padding-left:10px;
    padding-right:10px;
    width:250px;
    border-style:solid;
    border-width:5px;
    border-color:blue;
}
.divTableGreenBorder
{
    padding-top:10px;
    padding-bottom:200px;
    padding-left:10px;
    padding-right:10px;
    width:250px;
    border-style:solid;
    border-width:5px;
    border-color:green;
}
.divTableYellowBorder
{
    padding-top:10px;
    padding-bottom:200px;
    padding-left:10px;
    padding-right:10px;
    width:250px;
    border-width:5px;
    border-style:solid;
    border-color:yellow;
}
.divTableRedBorder
{
    padding-top:10px;
    padding-bottom:200px;
    padding-left:10px;
    padding-right:10px;
    width:250px;
    border-style:solid;
    border-width:5px;
    border-color:red;
}

2 个答案:

答案 0 :(得分:1)

看看这是否符合预期:http://jsfiddle.net/DerekL/YjzmY

您可以将代码缩减为:

var colors = [                       //first make a list of colors.
    "Blue",
    "Green",
    "Red",
    "Yellow"
    ],
    selected = "";                   //later used to store selected color

然后创建一个函数:

function seperate(arr,j){            //created a separate function
    return function(){               // to store i given by the loop.
        $(".divTable")
            .attr("class","divTable")
            .addClass("divTable" + arr[j]);
        selected = arr[j];
    }
}

function seperate_hover(arr,j){
    return function(){
        $("#button"+(j+1)).hover(function(){
                $(".divTable")
                .attr("class","divTable")
                .addClass("divTable"+arr[j]);
        },function(){
            $(".divTable")
                .attr("class","divTable")
                .addClass("divTable"+selected);  //change back to the selected color.
        });
    }
}

function doTheJob(arr) {
    for (var i = 0; i < arr.length; i++) {
        $("#button" + (i + 1)).on("click", seperate(arr,i));   //click
        seperate_hover(arr,i)();                               //hover
    }
}

doTheJob(colors);​                   //the script will now do the job for you

另外,请使用.on("click")代替.click()

答案 1 :(得分:0)

使用

$('target').on('click', function(){
     //add your code
});

而不是.click()