jQuery attr removeAttr 属性操作

时间:2023-03-08 17:31:19
jQuery attr removeAttr 属性操作

jQuery attr removeAttr 属性操作

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<meta charset="UTF-8">
<title>b index</title>
<link rel='stylesheet' type="text/css" href='b/css/bootstrap.css'>
</head>
<body style="margin:150px;">
<div id="div001">div001</div>
<div id="div002">div002</div>
<div id="div003">div003</div>
<div>
<button id="btn001">click me to use attr set string attr</button>
<button id="btn002">click me to use attr get attr</button>
<button id="btn003">click me to use attr set obj attr</button>
<button id="btn004">click me to use removeAttr string</button>
<button id="btn005">click me to use removeAttr obj</button>
<button id="btn006">click me to use attr set fn attr</button>
</div>
<script type="text/javascript" src="js/jquery-1.11.1.js"></script>
<script type="text/javascript" src="b/js/bootstrap.js"></script>
<script type="text/javascript" src="js/index031.js"></script>
</body>
</html>
$(function() {
$('#btn001').click(btn001Click);
$('#btn002').click(btn002Click);
$('#btn003').click(btn003Click);
$('#btn004').click(btn004Click);
$('#btn005').click(btn005Click);
$('#btn006').click(btn006Click);
});
function btn001Click() {
$('#div001').attr('a', 'a');
}
function btn002Click() {
// 如果没有设置a属性,会得到undefined;
var string = $('#div001').attr('a');
console.log(string);
}
function btn003Click() {
var obj = {
'a' : 'a',
'b' : 'b'
};
$('#div001').attr(obj);
}
function btn004Click() {
// 必须有输入参数,否则没有效果;
$('#div001').removeAttr();
// 输入'a','b'只会删除'a'的属性;输入['a','b']会报错;
$('#div001').removeAttr('a', 'b');
}
function btn005Click() {
var obj = {
'a' : 'a',
'b' : 'b'
};
// 输入一个对象会报错的;
try {
$('#div001').removeAttr(obj);
} catch (e) {
console.log(e);
}
}
function btn006Click() {
$('#div002').attr('title', function() {
// return this.id;
// 这样也是可以的;返回一个字符串就可以了;
return 'asdf';
});
}

var ids=$('div').attr('id');
得到的是第一个元素的id,还不是所有元素的id;

prop也是获得第一个元素的id;