Home >Web Front-end >JS Tutorial >jQuery how to select ID elements starting with specific characters

jQuery how to select ID elements starting with specific characters

coldplay.xixi
coldplay.xixiOriginal
2020-11-26 14:26:352867browse

How jQuery selects ID elements starting with specific characters: Use jquery’s [[attribute=value]] selector to achieve this. The [[attribute=value]] selector selects each element with the specified attribute and Element starting with the specified string.

jQuery how to select ID elements starting with specific characters

  • This method applies to all brands of computers

jQuery selects a specific Methods for ID elements starting with characters:

If you want to use jQuery to select elements whose ID starts with a specific character, you can use jquery's [attribute=value] selector. [attribute=value]The selector selects every element with the specified attribute and starting with the specified string.

Syntax:

$("[attribute^='value']")

Let’s take a look at the use of [attribute^=value] selector through examples.

Example: Select the element whose id starts with "demo" and change its background color

<!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> 
            $(&#39;#demo_p&#39;).text("点击按钮,选择id以“demo”开头的元素并更改背景色。"); 
            function GFG_Fun() { 
                $( "[id^=&#39;demo&#39;]" ).css("background-color", "pink"); 
                $(&#39;#demo_DOWN&#39;).text("测试文字!");
            } 
        </script>  
    </body>  
</html>

Output:

jQuery how to select ID elements starting with specific characters

Related Free learning recommendations: javascript (video)

The above is the detailed content of jQuery how to select ID elements starting with specific characters. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn