按钮改变它的颜色

时间:2015-08-28 18:28:48

标签: jquery button colors

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>moockup< /title>
        <style>
            .yellowBackground {
                background-color: yellow;
            }

            .redBackground {
                background-color: red;
            }
        </style>
    </head>

    <body>
        <button id="button" class="yellowBackground" id="thumb" >paina tästä< /button>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        <script>
            $(function() {
                $("#button").click(function() {
                    changeThumb();
                });
            });
            function changeThumb() {
                $("#thumb").toggleClass("yellowBackground redBackground");
            }
        </script>
    </body>
</html>

为什么这不起作用我想按下那个按钮,它必须在每次点击后将其颜色改为黄色和红色,我知道如何在javascript中进行操作,但我现在正在学习jQuery。

3 个答案:

答案 0 :(得分:1)

这是一个解决方案人:http://jsfiddle.net/leojavier/9psL0oa0/

<button class="yellowBackground" id="thumb" >test</button>

CSS

.yellowBackground {

background-color: yellow;
}

.redBackground {

background-color: red;
}

JS

$("#thumb").click(function() {

   $("#thumb").toggleClass("redBackground");

});

答案 1 :(得分:0)

希望这会回答你的问题。 小提琴[http://jsfiddle.net/57vss22x/3/]

jquery的

08-29 00:12:48.115: E/SQLiteLog(32608): (1) near "TABLEpersondetails": syntax error
08-29 00:12:48.119: D/AndroidRuntime(32608): Shutting down VM
08-29 00:12:48.135: E/AndroidRuntime(32608): FATAL EXCEPTION: main
08-29 00:12:48.135: E/AndroidRuntime(32608): Process: com.Prasad.formdetails, PID: 32608
08-29 00:12:48.135: E/AndroidRuntime(32608): android.database.sqlite.SQLiteException: near "TABLEpersondetails": syntax error (code 1): , while compiling: CREATE TABLEpersondetails(NametextmailIDtext primary keyPhonetext)
08-29 00:12:48.135: E/AndroidRuntime(32608):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
08-29 00:12:48.135: E/AndroidRuntime(32608):    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
08-29 00:12:48.135: E/AndroidRuntime(32608):    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
08-29 00:12:48.135: E/AndroidRuntime(32608):    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
08-29 00:12:48.135: E/AndroidRuntime(32608):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
08-29 00:12:48.135: E/AndroidRuntime(32608):    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
08-29 00:12:48.135: E/AndroidRuntime(32608):    at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
08-29 00:12:48.135: E/AndroidRuntime(32608):    at  android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
08-29 00:12:48.135: E/AndroidRuntime(32608):    at com.Prasad.formdetails.DatabaseHandler.onCreate(DatabaseHandler.java:25)
08-29 00:12:48.135: E/AndroidRuntime(32608):    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
08-29 00:12:48.135: E/AndroidRuntime(32608):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
08-29 00:12:48.135: E/AndroidRuntime(32608):    at com.Prasad.formdetails.DatabaseHandler.addPerson(DatabaseHandler.java:43)
08-29 00:12:48.135: E/AndroidRuntime(32608):    at com.Prasad.formdetails.MainActivity2$1.onClick(MainActivity2.java:32)
08-29 00:12:48.135: E/AndroidRuntime(32608):    at android.view.View.performClick(View.java:4761)
08-29 00:12:48.135: E/AndroidRuntime(32608):    at android.view.View$PerformClick.run(View.java:19767)
08-29 00:12:48.135: E/AndroidRuntime(32608):    at android.os.Handler.handleCallback(Handler.java:739)
08-29 00:12:48.135: E/AndroidRuntime(32608):    at android.os.Handler.dispatchMessage(Handler.java:95)
08-29 00:12:48.135: E/AndroidRuntime(32608):    at android.os.Looper.loop(Looper.java:135)
08-29 00:12:48.135: E/AndroidRuntime(32608):    at android.app.ActivityThread.main(ActivityThread.java:5312)
08-29 00:12:48.135: E/AndroidRuntime(32608):    at java.lang.reflect.Method.invoke(Native Method)
08-29 00:12:48.135: E/AndroidRuntime(32608):    at java.lang.reflect.Method.invoke(Method.java:372)
08-29 00:12:48.135: E/AndroidRuntime(32608):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
08-29 00:12:48.135: E/AndroidRuntime(32608):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

CSS

public class MainClass
{
    static void Main(string[] args)
    {
        // your code goes here
    }
}

HTML

$(".place").click(function () {
$(this).toggleClass("green");
});

答案 2 :(得分:0)

元素中不能有多个id属性。

.place { background-color: yellow; }
.place.green { background-color: red; }
<button id="button" class="place" id="thumb" >paina tästä</button>
$("#button").click(function() {
  changeThumb();
});
function changeThumb() {
  $("#button").toggleClass("yellowBackground redBackground");
}