JQuery Selector Prefix

From CoderLearner
Jump to: navigation, search

Description

It uses with an attribute name, and it selects a specified attribute with a value that the value is equal to a given string or starting with that string followed by a hyphen.

Example 1

In this example, use selector attribute prefix to select a none-standard attribute.

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Select</title>
      <script 
         type="text/javascript" 
         src="/lib/jquery-1.7.2.min.js">				 
      </script>
      <script>
         $().ready(function() {
            $('a[role2|="student"]').css("background-color","yellow");
         });
      </script>
   </head>
   <body> 
      <a href="#" id="sp_30" role2="student">John</a><br/>
      <a href="#" id="sp_31" role2="student">Christ</a><br/>
      <a href="#" id="sp_34" role2="teacher">Bob</a><br/>
      <a href="#" id="sp_35" role2="student">Weeks</a><br/>
   </body>
</html>

Try It

Example 2

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Select</title>
      <script 
         type="text/javascript" 
         src="/lib/jquery-1.7.2.min.js">				 
      </script>
      <script>
         $().ready(function() {
            $('a[hreflang|="en"]').append("<span>--English</span>");
         });
      </script>
   </head>
   <body> 
      <a href="#" hreflang="en">Mark 1</a><br/>
      <a href="#" hreflang="en-UK">Mark 2</a><br/>
      <a href="#" hreflang="english">Mark 3</a><br/>
      <a href="#" hreflang="en-SG">Mark 4</a><br/>
   </body>
</html>

Try It