日期:2014-05-20  浏览次数:20791 次

谁能帮我解释一下下面的jquery代码?
jQuery(document).ready(function(){
if(jQuery("div[id$='dataTable']").length>0){
jQuery("div[id$='dataTable']").each(function(index,item){jQuery(item).css('height','266px');});
}
var initHeight = jQuery("#content").css("height").replace("px","");
if(initHeight=="auto"||parseInt(initHeight)<50){
initHeight = "500";
}
jQuery("#content").css("height",initHeight+"px");
setRichCalendarStyle();
jQuery(":text").live("blur",function(){
var str = jQuery(this).val();
jQuery(this).val(jQuery.trim(str));
});
});
我想问一下"div[id$='dataTable']"是不是就是"#id='dataTable'"  就是找出div的id为dataTable的帖子??jQuery("#content")这个又是什么意思?
each(function(index,item))这里的两个参数分别代表什么?

还有上面的方法replace、trim、live、jQuery("#content").css("height",initHeight+"px");分别单表什么意思??
本人对jquery方法了解不多,希望哪个大虾能帮忙好好解释一下,谢谢了~~




还有下面一段代码:
function checkedAll(self,opts){
//the format of opts
//{formId:'userForm'}
if(typeof opts=='undefined' ||opts==null){
if(jQuery(self).attr("checked")){
jQuery(":checkbox:not(':first')").attr("checked",false).each(function(index,item){
jQuery(item).click();
});
}else{
jQuery(":checkbox:not(':first')").attr("checked",true).each(function(index,item){
jQuery(item).click();
});
}
}else{
var formId = opts.formId;
var needFire = opts.needFire;
if(isNull(formId)){
formId = "";
}
if(jQuery(self).attr("checked")){
jQuery("form[id$='"+formId+"'] :checkbox:not(':first')").attr("checked",false).each(function(index,item){
jQuery(item).click();
});
}else{
jQuery("form[id$='"+formId+"'] :checkbox:not(':first')").attr("checked",true).each(function(index,item){
jQuery(item).click();
});
}
}

}
下面一段代码在上面起的作用是什么?就是一个false、一个true。
jQuery(":checkbox:not(':first')").attr("checked",false).each(function(index,item){
jQuery(item).click();
});
}else{
jQuery(":checkbox:not(':first')").attr("checked",true).each(function(index,item){
jQuery(item).click();
});
}
function(index,item)的index代表的是第几个,而item是代表的具体的值是吗?

------最佳解决方案--------------------
1、"div[id$='dataTable']" :查找出 id=“dataTable”的div 元素。
2、jQuery("#content") : 查找出id="content"的元素,为什么不用$("#content")肯定是别名$冲突,重新换过一次别名,比如:var jQuery=jQuery.noConflict();
3、each(function(index,item)):index是元素的索引,item是该元素 ,不指定参数,利用$(this)也是一样的。
4、replace:顾名思义,用于替换,比如:$("image").attr("src").replace("size=60", "size=200");
5、trim:去掉元素内容的前后空格,比如:$("#image").trim($("#inp").val());
6、live:live() 方法为被选元素附加一个或多个事件处理程序,并规定当这些事件发生时运行的函数。比如:
当点击按钮时,隐藏或显示 p 元素:
$("button").live("click",function(){
  $("p").slideToggle();
});
自我感觉,没多大用,click也可以实现。
7、jQuery("#content").css("height",initHeight+"px"); 这个是为id="content"的div 重新指定该元素的高度。
8、jQuery(":checkbox:not(':first')").attr("checked",false).each(function(index,item){}
   :checkbox 表单选择器,不加冒号也可以实现,比如:$("input[name='a']")。
   :not 非
   :first  筛选第一个
   .attr()  获取属性checked的值
连起来就是,获取除了一个选项的所有checkbox 中没有选中的选项,为这些选项添加each事件。点击一下,,哦,我懂了,,就是反选。。呵呵,,跟你解答我自己也以学习学习。。加油~~

其实这个判断没有必要,直接这样子就可以了:
jQuery("form[id$='"+formId+"'] :checkbox:not(':first')").each(function(index,item){
jQuery(item).click();
});

你想下 是不是这样子的 。。
------其他解决方案--------------------