一、form表单:
1.判断checkbox是否选中
<input class="styled-checkbox" id="styled-checkbox-2" type="checkbox">
$('#styled-checkbox-2').get(0).checked //返回true false
2.判断单选框选中项
<input type="radio" name="optionsRadios" id="notneed" value="notneed">
$('input:radio[name="optionsRadios"]:checked').val();
3.清除输入内容的空格
$('#signin_name').val().trim()
注:无法清除字符串中的空格
(1) " " => "";
(2) " 123 345 " => " 123 345 "
----------------------------------------------------------------------------
.replace(/\ +/g,"") // 清除所有字符串中空格
$('#signin_lognum').val().replace(/\ +/g,"");
(1)" 12 34 " => "1234"
4.获取焦点和失焦
<input class="form-control" id="inputOldPass" type="password" placeholder="请输入原始密码">
$('#inputOldPass').focus(); //获取焦点
$('#inputOldPass').blur(); //失焦
$('#inputOldPass').blur(function(){
失焦事件
});
5.添加删除只读效果
$('#inputBao').attr("readonly", "readonly"); //设为只读
$('#inputBao').removeAttr("readonly"); //移除只读
6.<select>中指定某个<option>为禁用状态
$(".fhStyle option[value='neizhi']").attr("disabled", "disabled"); //设为禁用
$(".fhStyle option[value='neizhi']").removeAttr("disabled"); //移除禁用
7.<select>根据value设置选中状态
$("#selectClassType option[value='" + 参数 + "']").prop("selected", true);
8.改变输入框<textarea>行数
$('#json_'+num).attr("rows","8"); //拼接方式
$('#json_2').attr("rows","8"); //普通方式
9.获取所有<input>的value
$("textarea").each(
function(){
var value = $(this).val();
console.log(value);
}
);
二、标签选择器
1.将同一个类中的某一特定标签隐藏或显示
<span class="text-danger">*账号或密码错误</span>
<span class="text-danger">*两次密码不一致</span>
$('.text-danger').eq(0).hide();
$('.text-danger').eq(0).show();
2.定位兄弟、父级元素
$(obj).parent().next().children().removeAttr("readonly");
jQuery.parent(expr):找父亲节点,可以传入expr进行过滤,比如 ( " s p a n " ) . p a r e n t ( ) 或 者 ("span").parent()或者 ("span").parent()或者(“span”).parent(".class")
jQuery.parents(expr):类似于jQuery.parents(expr),但是是查找所有祖先元素,不限于父元素
jQuery.children(expr):返回所有子节点,这个方法只会返回直接的孩子节点,不会返回所有的子孙节点
jQuery.contents():返回下面的所有内容,包括节点和文本。这个方法和children()的区别就在于,包括空白文本,也会被作为一 个jQuery对象返回,children()则只会返回节点
jQuery.prev():返回上一个兄弟节点,不是所有的兄弟节点
jQuery.prevAll():返回所有之前的兄弟节点
jQuery.next():返回下一个兄弟节点,不是所有的兄弟节点
jQuery.nextAll():返回所有之后的兄弟节点
jQuery.siblings():返回兄弟姐妹节点,不分前后
jQuery.find(expr):跟jQuery.filter(expr)完全不一样:
jQuery.filter():是从初始的jQuery对象集合中筛选出一部分
jQuery.find():返回结果,不会有初始集合中的内容,比如 ( " p " ) . f i n d ( " s p a n " ) , 是 从 < p > 元 素 开 始 找 < s p a n > , 等 同 于 ("p").find("span"),是从<p>元素开始找<span>,等同于 ("p").find("span"),是从<p>元素开始找<span>,等同于(“p span”)
3.根据HTML标签选择元素
// input标签,type=hidden的,最后一个子元素的id
var e = $(obj).parent().children('input[type=hidden]:last-child').attr('id');
4.属性操作
// .attr( ); 设置img标签的宽为500
$("img").attr("width","500");
三、元素操作
1.添加元素
$("#boxLeft").append(str);
2.删除元素(删除boxLeft下所有内容,包括boxLeft)
$("#boxLeft").remove();
3.清空元素(清空boxLeft下所有内容,保留boxLeft本身,可与append搭配)
$("#boxLeft").empty();