属性过滤选择器,就是根据元素标签的属性筛选匹配元素。例如获取拥有id属性的p标签。 jQuery没有getElementsByName方法,可以使用属性选择器根据name选取input。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm17.aspx.cs" Inherits="WebApplication1.WebForm17" %>
<!DOCTYPE html>
<html xmlns="[http://www.w3.org/1999/xhtml](http://www.w3.org/1999/xhtml)">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="[http://libs.baidu.com/jquery/1.8.3/jquery.min.js](http://libs.baidu.com/jquery/1.8.3/jquery.min.js)"></script>
<style>
</style>
<script type="text/javascript">
function deal()
{
//获取带有ID属性的p标签
//alert($('p[id]').text());
//获取带有title属性的p标签
//alert($('p[title]').text());
//alert($('p[title=Foxdevelop]').text());
//alert($('input[name=username]').val());
alert($('p[title!=Foxdevelop]').text());
}
</script>
</head>
<body>
<p title="Foxdevelop">Foxdevelop</p>
<p id="site">www.foxdevelop.com</p>
<p title="介绍">免费 原创</p>
<input type="text" />
<input name="username" type="text" />
<input name="pwd" type="text" />
<input type="button" onclick="deal()" value="执行" />
</body>
</html>
Code language: HTML, XML (xml)
说明:
p[id]:选取存在id属性的p元素p[title]:选取存在title属性的p元素p[title=Foxdevelop]:选取title等于“Foxdevelop”的p元素input[name=username]:选取name等于username的inputp[title!=Foxdevelop]:选取title不等于“Foxdevelop”的p元素
Previous: 实现输入框获得焦点的时候背景颜色高亮效果
Next: 表单选择器