jQuery中[attribute^=value]选择器用法实例


本文实例讲述了jQuery中[attribute^=value]选择器用法。分享给大家供大家参考。具体分析如下:

此选择器能够选取属性值是以某些值开始的元素。

语法结构:

复制代码 代码如下:
$("[attribute^=value]")

参数列表:

参数 描述
attribute 属性名称。
value 元素数属性开始的值。
引号在大多数情况下是可选的。但在遇到诸如属性值包含"]"时,用以避免冲突。

实例代码:

实例一:

复制代码 代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.phpstudy.net/" />
<title>phpstudy</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>
<ul>
  <li id="third">欢迎来到phpstudy</li>
  <li>phpstudy欢迎您</li>
</ul>
<button>点击查看效果</button>
</body>
</html>

以上代码可以将li元素中id属性值以s开头的li元素的文本颜色设置为蓝色。

实例二:

复制代码 代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.phpstudy.net/" />
<title>phpstudy</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>
<ul>
  <li id="third">欢迎来到phpstudy</li>
  <li>phpstudy欢迎您</li>
</ul>
<button>点击查看效果</button>
</body>
</html>

从以上代码可以看出如何代码中含有"["或者"]"的时候,必须要带有引号,否则会造成匹配错误。

希望本文所述对大家的jQuery程序设计有所帮助。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3