前辈,我作为javascript新手在使用jquery的时候遇到了个问题,又不会百度搜,还望指教!

反转灵魂 2014-12-18 10:05:02

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="jquery.js"></script>
<script>
$(function(){
$(".picture").hover(function(){
$(this).addClass("pictureNew");
},function(){
$(this).removeClass("pictureNew");
})
})

$(document).ready(function(){
var $cr=$("#checkbox");
var cr=$cr[0];
$cr.click(function(){
if($cr.is(":checkd")){
alert("Thank you for your cooperation!");
}
})
})
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>login</title>
<link href="login.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div class="login-box">
<div class="head">
10秒注册,获得<span class="height">高薪</span>
</div>

<ul class="containt">
<li class="email-box">
邮箱 
<input class="email" type="email">
</li>

<li class="password-box">
密码 
<input class="password" style=type"password">
</li>

<li class="password2-box">
确认密码 
<input class="password2" style=type"password">
</li>

<li class="test-box">
验证码 
<input class="test" type="text">  
验证码图片  
<a href="">换一张</a>
</li>

<li class="reading">
<input class="checkbox" type="checkbox">
我已阅读并同意<a class="book" href="">《**网用户协议书》</a>
</li>

<li class="picture-box">
<a href=""><div class="picture"></div></a>
</li>
</ul>

<div class="foot">
<div class="number">已有账号?请<a href="">直接登录>></a></div>
</div>
</div>
</body>
</html>



为什么我点击checkbox没有弹窗提示呢,是不是JS代码中#不能得到class。还没入门,百度也不知道怎么搜,请前辈你帮我解决一下,感激不尽!
...全文
137 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
反转灵魂 2015-02-05
  • 打赏
  • 举报
回复
引用 4 楼 crying_boy 的回复:
1:$('.checkbox'):<input class="checkbox" type="checkbox"> 2:$('#checkbox'):<input id="checkbox" type="checkbox"> .class与#id的用法要搞清楚,这是最基本的了 =============== var $cr=$("#checkbox"); //假设ID=checkbox 的元素对象存在,那么返回的就是一个jQuery对象 var cr=$cr[0];//这里你取数组下标为0的,这时候cr已经不是一个jQuery对象了,相当于原生的:document.getElementById了。所以,这里不能这么写的 如果$cr对象在后面不再需要的话,那事件可以直接写为: $('.checkbox').click(function(){}) 当然,建议将class="checkbox"改为:id="checkbox",如果checkbox确实是一个css定义的话,那就加上ID定义。通过ID去查找对象比通过class去查找对象效率要高。然后将事件写为: $('#checkbox').click(function(){}) ============ 刚上传了一个jQuery1.7的中文帮助文档,你自己去下载了看看吧:http://download.csdn.net/download/crying_boy/8272579
谢谢你!
泡泡鱼_ 2014-12-18
  • 打赏
  • 举报
回复
1:$('.checkbox'):<input class="checkbox" type="checkbox"> 2:$('#checkbox'):<input id="checkbox" type="checkbox"> .class与#id的用法要搞清楚,这是最基本的了 =============== var $cr=$("#checkbox"); //假设ID=checkbox 的元素对象存在,那么返回的就是一个jQuery对象 var cr=$cr[0];//这里你取数组下标为0的,这时候cr已经不是一个jQuery对象了,相当于原生的:document.getElementById了。所以,这里不能这么写的 如果$cr对象在后面不再需要的话,那事件可以直接写为: $('.checkbox').click(function(){}) 当然,建议将class="checkbox"改为:id="checkbox",如果checkbox确实是一个css定义的话,那就加上ID定义。通过ID去查找对象比通过class去查找对象效率要高。然后将事件写为: $('#checkbox').click(function(){}) ============ 刚上传了一个jQuery1.7的中文帮助文档,你自己去下载了看看吧:http://download.csdn.net/download/crying_boy/8272579
Braska 2014-12-18
  • 打赏
  • 举报
回复

$(function(){
    $(".picture").hover(function(){
        $(this).addClass("pictureNew");
        },function(){
        $(this).removeClass("pictureNew");
        })
	
    $(".checkbox").click(function(){
       if(this.checked){
          alert("Thank you for your cooperation!");
       }
    })
})
最好统一写 要么jquery 要么js checkbox没有ID 所以不能用#checkbox $cr.is(":checkd") 改成$cr.is(":checked") 细心点。
波儸密 2014-12-18
  • 打赏
  • 举报
回复
var $cr=$("#checkbox"); 改为 var $cr=$(".checkbox");
反转灵魂 2014-12-18
  • 打赏
  • 举报
回复
已解决!加个id就好。

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧