jquery13 attr() prop() val() addClass()等 : 对元素属性的操作
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<script src="jquery-2.0.3.js"></script>
<script> jQuery.fn.extend({
attr
removeAttr
prop
removeProp
addClass
removeClass
toggleClass
hasClass
val
});
jQuery.extend({
valHooks
attr
removeAttr
attrHooks
propFix
prop
propHooks
}); $(function(){
$('#div1').attr('title','hello');
alert( $('#div1').attr('id') ); $('#div1').prop('title','hello');
alert( $('#div1').prop('id') );
----------------------------------------------------------
//setAttribute()
//. | [] var oDiv = document.getElementById('div1');
oDiv.setAttribute('title','hello');
oDiv.title = 'hello1';
oDiv['title'] = 'hello2';
----------------------------------------------------------
$('#div1').attr('miaov','妙味');//可以加自定义属性
$('#div1').prop('miaov','妙味');//不可以加自定义属性 alert($('#div1').attr('miaov'));//可以获取自定义属性
alert($('#div1').prop('miaov'));//不可以获取自定义属性
---------------------------------------------------------
alert($('#div1').prop('href'));//a标签href属性,
alert($('#div1').attr('href'));
---------------------------------------------------------
$('#div1').removeAttr('id');
alert($('#div1').attr('id'));
//对自带属性用attr操作,
$('#div1').removeProp('id');
alert($('#div1').prop('id'));//删不掉,
---------------------------------------------------------
$(document).attr('title','hello'); $('#div1').attr('miaov',null);//调用remove
//checked|selected|async|autofocus|autoplay|controls|defer|disabled|hidden|ismap|loop|multiple|open|readonly|required|scoped alert( $('input').attr('checked') ); //checked
alert( $('input').prop('checked') ); //true
$('input').attr('checked','checked');//没问题
$('input').attr('checked',true);//没问题,做兼容了 $('#div1').removeAttr('class'); }); </script>
</head> <body>
<a id="div1" miaov="妙味" class="box" href="miaov.com">aaaaaaaaaaaaaaaaa</a>
<input type="checkbox">
<input type="text" tabindex="2">
<input type="text" tabindex="1">
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<script src="jquery-2.0.3.js"></script>
<script> $(function(){
$('#div1').addClass('box2 box3');
$('#div1').removeClass('box3'); $('#div1').toggleClass('box3');//切换,有就删除没有就添加, alert( $('#div1').hasClass('box3') ); $('div').addClass(function(index){
alert(index);
return 'box'+index;
}); alert( 1 || 0 && 2 );//1,先后面在前面,&&优先级高于|| $('#div1').removeClass('box1 box2');
$('#div1').removeClass('');//全部删除 $('#div1').toggleClass('box2 box3')//有就删除没有就添加
$('#div1').toggleClass('box2 box3',true);//有没有都是add
$('#div1').toggleClass('box2 box3',false);//有没有都是删除 $('#div1').toggleClass(false);//已有的class全部删除
$('#div1').toggleClass(true);//删除的class全部添加进去
}); </script>
</head> <body>
<div class="">aaaaaaaaaaaa</div>
<div class="">aaaaaaaaaaaa</div>
<div class="">aaaaaaaaaaaa</div> <div id="div1" class="box1 box2">aaaaaa</div>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<script src="jquery-2.0.3.js"></script>
<script> $(function(){ alert( $('#input1').val() );
$('#input1').val('hi');
alert( $('#input1').val() ); //兼容处理 valHooks : option select radio checkbox
alert( $('checkbox').get(0).type );//checkbox
alert( $('select').get(0).type );//select-one alert( $('option').eq(0).get(0).nodeName );
-------------------------------------------------------------------------
$('#input2').click(function(){ alert( $('select').val() );//select多选时弹出数组 });
$('#select').val(111);//111被选中了
----------------------------------------------------------------------
$('#input1').val(123123); $('#input2').val(['hello']); $('select').val(222);
}); window.onload = function(){ //window.onload 是原生写法,$(function()是jQuery写法
var oP = document.getElementsByTagName('option')[0];
alert( oP.attributes.value.specified );
}; </script>
</head> <body>
<input type="text" id="input1" value="hello">
<select multiple>
<option>111</option>
<option disabled>222</option>
<option>333</option>
</select>
<input type="checkbox" id="input2" value="hello">
</body>
</html>
jquery13 attr() prop() val() addClass()等 : 对元素属性的操作的更多相关文章
- jQuery07源码 (3803 , 4299) attr() prop() val() addClass()等 : 对元素属性的操作
var nodeHook, boolHook, rclass = /[\t\r\n\f]/g, rreturn = /\r/g, rfocusable = /^(?:input|select|text ...
- jquery之 css()方法。用法类似的有attr(),prop(),val()
[注]attr(),prop(),val()的用法结构和css()一致,可参考 css()函数用于设置或返回当前jQuery对象所匹配的元素的css样式属性值. 该函数属于jQuery对象(实例).如 ...
- jQuery.attributes源码分析(attr/prop/val/class)
回顾 有了之前的几篇对于jQuery.attributes相关的研究,是时候分析jQuery.attr的源码了 Javascript中的attribute和property分析 attribute和p ...
- [Web 前端 ] Jquery attr()方法 获取或修改 对象的属性值
cp from : https://blog.csdn.net/gf771115/article/details/18086707 jquery中用attr()方法来获取和设置元素属性,attr是at ...
- jQuery元素属性操作
在jQuery中,用attr()方法来获取或者设置元素属性,removeAttr()方法用来删除元素属性. attr() 实例: var $para=$('p');//获取<p>节点 va ...
- Javascript操作元素属性方法总结
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- jQuery笔记三——text/html/val/attr/prop
1.获得内容 三个简单实用的用于 DOM 操作的 jQuery 方法: text() - 设置或返回所选元素的文本内容 html() - 设置或返回所选元素的内容(包括 HTML 标记) val() ...
- jQuery 属性操作attr().prop().text().html().val()
这些方法用于获取和设置 DOM 元素的属性. 一.attr(): <!--样式:在style里面写的,用css来操作.--> <!--属性:在元素里面写的,用attr方法操作.--& ...
- jQuery操纵DOM元素属性 attr()和removeAtrr()方法使用详解
jQuery操纵DOM元素属性 attr()和removeAtrr()方法使用详解 jQuery中操纵元素属性的方法: attr(): 读或者写匹配元素的属性值. removeAttr(): 从匹配的 ...
随机推荐
- Python TurtleWorld configuration and simple test
TurtleWorld provides a set of functions for drawing lines by steering turtles around the screen. You ...
- 12.红黑树set
#include <iostream> //红黑树(自动保证平衡,自动生成平衡查找树) #include <set> #include <cstring> #inc ...
- HTML 表格 做个人简历
根据老师上课讲的常用标签与表格的应用 终于做出了第一个网页版的个人简历 虽然作出来了 但是感觉其中方法有点儿问题 还需要进一步的改进中…… <!DOCTYPE html PUBLIC " ...
- ES6中includes、startsWith、endsWith
es6新增includes:返回布尔值,表示是否找到字符串.startsWith:返回布尔值,表示字符串是否在源字符串的头部位置.endsWith:返回布尔值,表示参数字符串是否在源字符串尾部. va ...
- 【C#Windows 服务】 《三》Timer设置
一.工具: VS2015+NET Framework4.5. 二.操作: 1.计时器设置: 2.日志代码: 三.代码: 1.日志代码: 1 /// <summary> 2 /// Wind ...
- core组件进阶
访问图像像素 存储方式 BGR连续存储有助于提升图像扫描速度. isContinuous()判断是否是连续存储. 颜色空间缩减 仅用这些颜色中具有代表性的很小的部分,就足以达到同样的效果. 将现有颜色 ...
- 搭建Lvs负载均衡群集
一.Lvs详解 lvs内核模型 1.模型分析 用户访问的数据可以进入调度器 匹配调度器分配的虚拟IP|IP+端口(路由走向) 根据调度器的算法确定匹配的服务器 2.调度条件:基于IP.基于端口.基于内 ...
- HDU 5068 Harry And Math Teacher 线段树+矩阵乘法
题意: 一栋楼有n层,每一层有2个门,每层的两个门和下一层之间的两个门之间各有一条路(共4条). 有两种操作: 0 x y : 输出第x层到第y层的路径数量. 1 x y z : 改变第x层 的 y门 ...
- Java基础学习总结(16)——Java制作证书的工具keytool用法总结
一.keytool的概念 keytool 是个密钥和证书管理工具.它使用户能够管理自己的公钥/私钥对及相关证书,用于(通过数字签名)自我认证(用户向别的用户/服务认证自己)或数据完整性以及认证服务.在 ...
- 【转】 基于C#.NET的高端智能化网络爬虫
[转] 基于C#.NET的高端智能化网络爬虫 前两天朋友发给我了一篇文章,是携程网反爬虫组的技术经理写的,大概讲的是如何用他的超高智商通过(挑衅.怜悯.嘲讽.猥琐)的方式来完美碾压爬虫开发者.今天我就 ...