JQuery属性操作之attr()和prop()的区别
代码示例:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>attr() 和 prop()区别</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function () {
//获取第一个input
var input_first = $("input").first();
//undefined 因为attr是获取的这个对象属性节点的值, 而词视没有这个属性节点, 所以输出undefined
console.log(input_first.attr("style"));
//输出CSSStyleDeclaration对象, 对于一个DOM对象, 是具有原生的style对象属性的,所以输出了style对象
console.log(input_first.prop("style"));
console.log(document.getElementById("test1").style); $("button").click(function () {
alert(input_first.prop("checked")? "男": "女");
})
})
</script>
</head>
<body>
<input type="radio" id="test1" name="sex" checked>男
<input type="radio" id="test2" name="sex">女
<button>提交</button>
</body>
</html>
什么时候使用attr(), 什么时候使用prop()?
(1). 当只有true或者false时, 使用prop();
(2). 其他则使用attr()
JQuery属性操作之attr()和prop()的区别的更多相关文章
- jQuery中操作属性的方法attr与prop的区别
attr 与 prop 都可以对某个属性进行获取和设置的操作,二者的用法相同: <script src = 'jQuery.js'></script> <script&g ...
- jquery 获取自定义属性(attr 和 prop的区别)
在高版本的jquery引入prop方法后,什么时候该用prop?什么时候用attr?它们两个之间有什么区别?这些问题就出现了. 关于它们两个的区别,网上的答案很多.这里谈谈我的心得,我的心得很简单: ...
- Jquery学习笔记: attr和 prop的区别,以及为html标签自定义属性
一.自定义html标签属性 对于html文件中的html标签,可以自定义属性,如: <a href="#" id="link1" action=" ...
- jQuery学习笔记:attr()与prop()的区别
先看看官方文档是如何解释两者之间功能差异的: attr() Get the value of an attribute for the first element in the set of matc ...
- jQuery属性操作之.attr()
目录 .attr() 调用形式:$("xxx").attr(name) 调用形式:$("xxx").attr(name,value); 调用形式:$(" ...
- jquery中attr和prop的区别、 什么时候用 attr 什么时候用 prop (转自 芈老头 )
jquery中attr和prop的区别. 什么时候用 attr 什么时候用 prop 在高版本的jquery引入prop方法后,什么时候该用prop?什么时候用attr?它们两个之间有什么区别?这 ...
- jQuery源代码学习之八——jQuery属性操作模块
一.jQuery属性模块整体介绍 jQuery的属性操作模块分四个部分:html属性操作,dom属性操作,类样式操作,和值操作. html属性操作(setAttribute/getAttribute) ...
- jQuery中.attr()和.prop()的区别
之前学习jQuery的时候,学习到了两种取得标签的属性值的方法:一种是elemJobj.attr(),另一种是elemJobj.prop().而在学习JS的时候,只有一种方法elemObj.getAt ...
- jquery中attr和prop的区别(转)
在网络上看到这样一篇关于jquery中attr和prop的区别文章,觉得不错,所以转载了. 在jQuery 1.6中,.attr()方法查询那些没有设置的属性,则会返回一个undefined.如果你要 ...
随机推荐
- Java HashMap、HashTable与ConCurrentHashMap
一.Java中数据存储方式最底层的两种结构 1.数组:存储空间连续,寻址迅速,增删较慢.(代表:ArrayList) 2.链表:存储空间不连续,寻址慢,增删较快.(代表:LinkedList) 二.哈 ...
- stuff拼接字符串
stuff stuff(param1,startIndex,length,param2) 说明:将param1中自startIndex(SQL中都是从1开始,而非0)起,删除length个字符,然后用 ...
- linq to sql之Distinct
Distict用来排除相同序列中元素的,对于基础类型,可以直接使用Distinct,如:int[] a = {1, 2, 2, 3, 3, 3, 4};var reslut = a.Distinct( ...
- Linux之curl
简介 curl 是常用的命令行工具,用来请求 Web 服务器.它的名字就是客户端(client)的 URL 工具的意思. 它的功能非常强大,命令行参数多达几十种.如果熟练的话,完全可以取代 Postm ...
- Editplus code
网上一大堆,垃圾也是一大堆,保留一个真正的,提高效率 原文链接:https://blog.csdn.net/anhldd/article/details/85088715 Vovan 3AG46-JJ ...
- js jquery 动态添加表格
for循环将你要添加的标签写上,然后直接var talbeAdd=""for(){ tableAdd+="<tr><td>这儿写你要添加的内容&l ...
- windows 下Nginx 入门
验证配置是否正确: nginx -t 查看Nginx的版本号:nginx -V 启动Nginx:start nginx 快速停止或关闭Nginx:nginx -s stop 正常停止或关闭Nginx: ...
- IPC之namespace.c源码解读
// SPDX-License-Identifier: GPL-2.0 /* * linux/ipc/namespace.c * Copyright (C) 2006 Pavel Emelyanov ...
- 使用ViewFlipper实现图片轮播
public class MainActivity extends AppCompatActivity { private ViewFlipper flipper; //背景图片int[] id pr ...
- 执行nvidia-smi出错
执行nvidia-smi出错 NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make su ...