jQuery Tutorial Tutorials - jQuery :target selector

:target selector

The jQuery :root selector is used to select the target element indicated by the fragment identifier of the document's URI.

jQuery :target selector Syntax

$(':target')

jQuery :target selector Examples

Following is a simple example which makes use of :target Selector.

Note:

  1. Please save the code to a html file then open it with fragment identifier intro
    For example:
    http://127.0.0.1:5500/test.html#intro
  2. Please use the latest version jQuery to test :target selector
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>jQuery root selector example - Lautturi</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
    <style>*:target { color : red }</style>
    <p id="intro">Introduce</p>
    <script>
        $(function(){
            console.log($( ":target" )); //[<p id="intro">Introduce</p>]
            console.log($( ":target" ).length); // 1
        });        
    </script>
</body>
</html>
Date:2019-08-18 02:20:51 From:www.Lautturi.com author:Lautturi