使用jquery获取和设置HTML元素的属性
var =jquery对象.attr(属性名)(作用:可以获取HTML的属性)
jquery对象.attr(属性名,属性值)(设置HTML元素的属性)
<html>
<head>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
$("a").attr("targer","_blank");
});
</script>
</head>
<body>
<a href="http://www.sina.com.cn">新浪</a>
<a href="http://www.baidu.com.cn">百度</a>
</body>
</html>
“_blank”设置为在新窗口打开网页;
使用jquery删除HTML元素的属性
jquery对象。removeAttr(属性名)
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("a").click(function(){
$(this).next().removeAttr("disabled");
.focus()
.val("现在可以编辑了")
});
});
</script>
</head>
<body>
<button>启用</button>
<input type="text" disabled="disabled" value="现在不能编辑"/>
</body>
</html>
使用jquery在网页中添加追加内容
jquery对象.append(追加内容)(作用:向HTML元素追加内容)
jquery对象.before(追加内容)(作用:向HTML元素前面追加内容)
jquery对象.after(追加内容)(作用:向HTML元素后面追加内容)
对使用jQuery操作HTML元素(1)补充:
- html()方法:读取html的文本内容(包括标签)。例如:<a>hello</a> 获取内容为<a>hello</a>;
- text()方法:读取html的文本内容和子元素内容(不包括标签)。例如:<a>hello</a> 获取内容为hello;
- val()方法:读取或修改有value值的表单内容。
- remove()方法:将标签与内容移除。例如:<a>hello</a> 结果为将 <a>hello</a> 都删除;
- empty()方法:只讲内容移除。例如:<a>hello</a> 结果为将 hello 。