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(): 从匹配的 ...
随机推荐
- java9新特性-14-多分辨率图像 API
1.官方Feature 251: Multi-Resolution Images 263: HiDPI Graphics on Windows and Linux 2.产生背景 在Mac上,JDK已经 ...
- 鼠标滑过,解决ul下 li下a的背景与父级Li不同宽的问题
我们在写导航或者页面有超链接的地方,有一些是需要超链接的背景和Li的宽度一样的.但是,却没有达到这种效果?为什么? 我们做的效果图:如下 期望的效果:如下 出现这样的原因:由于a是个行内元素,它没有宽 ...
- ByteArray的操作总结(复制、打印、位运算)
1. 字节数组的复制 Method A:Array.Clone() Clone返回的是Object对象,需要强类型转换:Clone会创建一个新的对象,并将value赋给dec ]; ]; dst = ...
- vuejs helloworld
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Remmina:一个 Linux 下功能丰富的远程桌面共享工具(转载)
Remmina:一个 Linux 下功能丰富的远程桌面共享工具 作者: Aaron Kili 译者: LCTT geekpi | 2017-05-10 09:05 评论: 2 收藏: 4 Remm ...
- python描述符和属性查找
python描述符 定义 一般说来,描述符是一种访问对象属性时候的绑定行为,如果这个对象属性定义了__get__(),__set__(), and __delete__()一种或者几种,那么就称之为描 ...
- 使用SVN+Axure RP 8.0创建团队项目
一.使用到的工具:VisualSVN Server --SVN服务器:https://www.visualsvn.com/server/ Axure RP 8.0 :http://www.downc ...
- ArcGIS api for javascript——地理处理任务-瓶中信
描述 如果在海洋中丢下一个瓶子,本例使用颗粒追踪模型显示指定的天数后瓶子在的地方.首先,输入一个追踪瓶子的天数.然后单击按钮并在海洋里的任意地方画一个点来开始模型.几秒以后将看到一条线出现描述瓶子将去 ...
- nginx和apache
apache所占用的内存资源较多,并且处理较慢 apache的全部模块都支持动静态编译 apache对Fcgi的支持不好 apache不支持epoll apache相对于nginx是一个庞然大物 ng ...
- 如何快速复制Windows警告提示消息对话框内容
凡是使用过计算机的朋友,都遇到过系统发出的警告提示消息对话框,如图所示. 哇!好长的一串英文错误警告,这要手写到什么时候呢?不!现在不用这么麻烦了. 你只要鼠标选中这个提示框Ctrl+C,然后打开你的 ...