English 中文(简体)
B. 改变边界和表面图并 h取
原标题:Changing border and background colors of a table and hovering over them

我试图用一个 but子的点击来改变桌子的背景和边界。 我也在试图改变肤色,把颜色 over上。 如果是先去做的话,我就开始工作。 我面临的问题是,当我点一个纽子改变背景颜色时,我不能点击任何其他纽子,以改变这一具体纽子。 例如,我有四个县,即蓝色、绿色、黄色、红色。 如果我点击蓝 but子,其背景将改变为蓝色,那么如果我选择点击其他有色的 but子,那就不会奏效,而且在我点击任何 but子之后,我的 h不会再工作。 此外,如何减少政变。 如果我增加更多彩色纽芬兰语,那么按比率计算,只有更平等的法典。

 <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 is

.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;
}
问题回答

See if this is what you expected: http://jsfiddle.net/DerekL/YjzmY

You can reduce you code into:

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

Then make a function:

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

Use

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

页: 1





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签