2

我正在尝试通过单击按钮来更改表格的背景和边框。我还尝试通过将鼠标悬停在按钮上来更改颜色。我让悬停工作,如果我先做的话。我遇到的问题是,当我单击按钮更改背景颜色时,我无法单击任何其他按钮更改为该特定按钮颜色。例如,我有四个按钮,蓝色、绿色、黄色、红色。如果我单击蓝色按钮,背景将变为蓝色,然后如果我选择单击其他颜色的按钮,它将不起作用,并且在单击任何按钮一次后,我的悬停将不再起作用。另外,如何减少耦合。如果我添加更多颜色按钮,我会按照我的速度,这只会等于更多的代码行。

 <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;
}
4

2 回答 2

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().

于 2012-05-13T20:48:28.890 回答
0

利用

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

代替.click()

于 2012-05-13T20:49:07.937 回答