返回值:Array[attribute^=value]
概述
匹配给定的属性是以某些值开始的元素
参数
属性名
属性值。引号在大多数情况下是可选的。但在遇到诸如属性值包含"]"时,用以避免冲突。
示例
描述:
查找所有 name 以 'news' 开始的 input 元素
HTML 代码:
1 | <input name= "newsletter" /> <input name= "milkman" /> <input name= "newsboy" />
|
登录后复制
jQuery 代码:
1 | $( "input[name^='news']" )
|
登录后复制
结果:
1 | [ <input name= "newsletter" />, <input name= "newsboy" /> ]
|
登录后复制
选择器能够选取属性值是以某些值开始的元素。
语法结构:
参数列表:

实例代码:
实例一:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <!DOCTYPE html>
<html>
<head>
<meta charset= " utf-8" >
<meta name= "author" content= "http://www.jb51.net/" />
<title></title>
<script type= "text/javascript" src= "mytest/jQuery/jquery-1.8.3.js" ></script>
<script type= "text/javascript" >
$(document).ready( function (){
$( "button" ).click( function (){
$( "li[id^=s]" ).css( "color" , "blue" );
})
})
</script>
</head>
<body>
<ul>
<li id= "first" >html专区</li>
<li id= "second" title= "jquery" >Jquery专区</li>
</ul>
<button>点击查看效果</button>
</body>
</html>
|
登录后复制
以上代码可以将li元素中id属性值以s开头的li元素的文本颜色设置为蓝色。
实例二:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <!DOCTYPE html>
<html>
<head>
<meta charset= " utf-8" >
<meta name= "author" content= "http://www.jb51.net/" />
<title></title>
<script type= "text/javascript" src= "mytest/jQuery/jquery-1.8.3.js" ></script>
<script type= "text/javascript" >
$(document).ready( function (){
$( "button" ).click( function (){
$( "li[id^=[s]" ).css( "color" , "blue" );
});
});
</script>
</head>
<body>
<ul>
<li id= "first" >html专区</li>
<li id= "[second" title= "jquery" >Jquery专区</li>
</ul>
<button>点击查看效果</button>
</body>
</html>
|
登录后复制
从以上代码可以看出如何代码中含有"["或者"]"的时候,必须要带有引号,否则会造成匹配错误。
以上是jQuery选择器[attribute^=value]的具体使用详解的详细内容。更多信息请关注PHP中文网其他相关文章!