jQuery:属性选择器

一,Attribute Selector [name="value"]

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<script src="../static/js/jquery-3.7.1.min.js"></script>

<body>
  <div>
    <input type="radio" name="user" value="username">
    <span>username</span>
  </div>

  <div>
    <input type="radio" name="user" value="age">
    <span>age</span>
  </div>


  <script>
    console.log($("input[value='username']").next());
    $("input[value='username']").next().html("new username");
  </script>

</body>

</html>

二,Attribute Selector [name|="value"] 

选择指定属性值等于给定字符串或以该字符串为前缀(该字符串后跟一个连字符“-” )的元素

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<script src="../static/js/jquery-3.7.1.min.js"></script>

<body>
  <input type="text" name="sub-string">

  <script>
    console.log($("input[name |= 'sub']"));
  </script>

</body>

</html>

三,Attribute Selector [name*="value"] 

选择指定属性具有包含一个给定的子字符串的元素。(选择给定的属性是以包含某些值的元素)

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<script src="../static/js/jquery-3.7.1.min.js"></script>

<body>
  <input type="text" name="sub-string">
  <input type="text" name="a-string">
  <input type="text" name="bstring">
  <input type="text" name="cstring">
  

  <script>
    console.log($("input[name *= 'string']"));
  </script>

</body>

</html>

四,Attribute Selector [name~="value"]

选择指定属性用空格分隔的值中包含一个给定值的元素 

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<script src="../static/js/jquery-3.7.1.min.js"></script>

<body>
  <input type="text" name="sub-string">
  <input type="text" name="a string">
  <input type="text" name="bstring">
  <input type="text" name="cstring">
  

  <script>
    console.log($("input[name ~= 'string']"));
  </script>

</body>

</html>

五,Attribute Selector [name$="value"]

选择指定属性是以给定值结尾的元素。这个比较是区分大小写的

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<script src="../static/js/jquery-3.7.1.min.js"></script>

<body>
  <input type="text" name="sub-string">
  <input type="text" name="a string">
  <input type="text" name="bstring">
  <input type="text" name="cstring">


  <script>
    console.log($("input[name $= 'string']"));
  </script>

</body>

</html>

六,Attribute Selector [name^="value"] 

 选择指定属性是以给定字符串开始的元素

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<script src="../static/js/jquery-3.7.1.min.js"></script>

<body>
  <input type="text" name="sub-string">
  <input type="text" name="a string">
  <input type="text" name="bstring">
  <input type="text" name="cstring">


  <script>
    console.log($("input[name ^= 'a']"));
  </script>

</body>

</html>

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值