在 jQuery 中, 可以使用 [attribute^=value] 选择器来选择以特定字符开头的 ID 元素. 下面本篇文章给大家介绍一下. 有一定的参考价值, 有需要的朋友可以参考一下, 希望对大家有所帮助.
[attribute^=value] 选择器选取每个带有指定属性且以指定字符串开头的元素.
语法:
$("[attribute^='value']")
示例: 选择 id 以 "demo" 开头的元素并更改其背景颜色
- <!DOCTYPE html>
- <HTML>
- <head>
- <meta charset="utf-8">
- <style>
- #demo_div { background: green; height: 100px; width: 200px; margin: 0
- auto; color: white; line-height: 100px; }
- </style>
- <script src="https://code.jquery.com/jquery-1.10.2.js">
- </script>
- </head>
- <body style="text-align:center;">
- <div id="demo_div">
- 这是 div 盒.
- </div>
- <p id="demo_p" style="font-size: 19px; font-weight: bold;">
- </p>
- <button onClick="GFG_Fun()">
- 点击这里
- </button>
- <p id="demo_DOWN" style="color: white; font-size: 24px; font-weight: bold;">
- </p>
- <script>
- $('#demo_p').text("点击按钮, 选择 id 以"demo "开头的元素并更改背景色.");
- function GFG_Fun() {
- $("[id^='demo']").CSS("background-color", "pink");
- $('#demo_DOWN').text("测试文字!");
- }
- </script>
- </body>
- </HTML>
更多 web 开发 https://www.html.cn/ 知识, 请查阅 HTML 中文网 !!
来源: http://www.css88.com/qa/jquery/17109.html