使用jQuery更改背景颜色(css属性) [英] Change Background color (css property) using Jquery

查看:95
本文介绍了使用jQuery更改背景颜色(css属性)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在单击时更改背景颜色.这是我尝试过的代码工作.请帮助我:)

I want to Change the background colour on click . This is my code work that i tried.pls help me out :)

   <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">

$(document).ready(function(){

$(#co).click(change()
{
$(body).css("background-color":"blue");
});
}); 

Css代码

body
{
background-color:red;
}

身体代码

      <body>

    <div id="co" click="change()">

hello

    </div>

推荐答案

您使用的是冒号而不是逗号.试试:

You're using a colon instead of a comma. Try:

$(body).css("background-color","blue");

您还需要将ID括在引号中,否则它将查找名为 #co

You also need to wrap the id in quotes or it will look for a variable called #co

$("#co").click(change()

这里还有更多问题. click 不是HTML属性.您需要 onclick (这是多余的).试试这个:

There are many more issues here. click isn't an HTML attribute. You want onclick (which is redundant). Try this:

<div id="co"> <!-- no onclick method needed -->
<script>
$(document).ready(function() {
    $("#co").click(function() {
        $("body").css("background-color","blue"); //edit, body must be in quotes!
    });
});
</script>

您试图调用未定义的方法.好像您要在回调语句中声明它?我不确定.但是,请将此与您的代码进行比较,看看有什么区别.

You were trying to call an undefined method. It looks like you were trying to declare it inside the callback statement? I'm not sure. But please compare this to your code and see the differences.

http://jsfiddle.net/CLwE5/演示小提琴

这篇关于使用jQuery更改背景颜色(css属性)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆