日期:2014-05-16  浏览次数:20400 次

初学jquery的常件错误,和大家分享下~
(1)

事件括号里,忘记写function( ){ }函数体

$("#shan").click(
var count=$(":checkbox").length
alert(count);
)

$("p").each( // each里也别忘记了function函数体

  function( ){

alert(1);

  }

)

(2)

$(":checkbox.not('#all')").length,只有“is”判断才是用点接$(":checkbox.is(:checked)"),not是用冒号来衔接,正确写法是:

$(":checkbox:not('#all')").length

(3)

this.id等价于$(this).attr("id"),于是就认为this.remove( )也可以,其实remove( )是jquery的专有方法,所以只能$(this).remove( )。

(4)

css("color","red")这里的color必须带引号,还有个地方前面那个参数必须带引号,那便是bind事件里。如:

$("#f").bind( focus,function( ){ 这里的focus就必须带引号,否则会有错误,改成"focus"
$(this).css("color","red");
})

(5)

 <script type="text/javascript">
$("#f").mouseover(
function( ){
$(this).attr("color","red"); // 不是属性,干嘛要用attr
}
)
  </script>

把css混淆成为属性,应该改成$(this).css("color","red");

(6)

$("#f").triggle("focus");

不是triggle而是trigger。

(7)

var a="<div>wm</div>"; // 错在没加$( ),改成$("<div>wm</div>")
$("#s").append(a);

(8)

 <input type="text" id="f">

var a=$("<span>wm</span>");
$("#f").append(a);  

// 错在append是在内部添加,而input是个输入框,根本就没有内部,所以要用before,after这样的外部方式添加,改成$("#f").after(a);  


------解决方案--------------------
接分的,就是有点少不够分啊