jQuery選擇以特定字符開(kāi)頭的ID元素的方法:使用jquery的【[attribute=value]】選擇器來(lái)實(shí)現(xiàn),【[attribute=value]】選擇器選取每個(gè)帶有指定屬性且以指定字符串開(kāi)頭的元素。
-
該方法適用于所有品牌電腦
jQuery選擇以特定字符開(kāi)頭的ID元素的方法:
想要使用jQuery選擇ID以特定字符開(kāi)頭的元素,可以使用jquery的[attribute=value]選擇器來(lái)實(shí)現(xiàn)。[attribute=value]
選擇器選取每個(gè)帶有指定屬性且以指定字符串開(kāi)頭的元素。
語(yǔ)法:
$("[attribute^='value']")
下面通過(guò)示例來(lái)看看[attribute^=value]
選擇器的使用。
示例:選擇id以“demo”開(kāi)頭的元素并更改其背景顏色
<!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()">點(diǎn)擊這里</button> <p id = "demo_DOWN" style = "color: white; font-size: 24px; font-weight: bold;"> </p> <script> $('#demo_p').text("點(diǎn)擊按鈕,選擇id以“demo”開(kāi)頭的元素并更改背景色。"); function GFG_Fun() { $( "[id^='demo']" ).css("background-color", "pink"); $('#demo_DOWN').text("測(cè)試文字!"); } </script> </body> </html>
輸出:
相關(guān)免費(fèi)學(xué)習(xí)推薦:javascript(視頻)