JavaScript can't click and can't get the attributes of every a.

1. I want to use the onclick click event to get the data-id value of each an attribute, but there is no response. Is there any way to get the value of each attribute?

Please take a look at the following code:

html file

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <ul>
        <li>
            <a id="showClick" href="" data-id="1">dd</a>
        </li>
        <li>
            <a id="showClick" href="" data-id="2">ee</a>
        </li>
        <li>
            <a id="showClick" href="" data-id="3">gg</a>
        </li>
        <li>
            <a id="showClick" href="" data-id="4">xx</a>
        </li>
    </ul>

    <script type="text/javascript">
        window.onload = function () {
            var showClick = document.getElementById("showClick");
            for (var i = 0; i < showClick.length; iPP) {
                (function (i) {
                    showClick[i].onclick = function (event) {
                        event.preventdefault() 
                        alert(showClick.getAttribute("data-id"));
                    }
                })(i);
            }
     </script>
Jul.09,2021

getElementById can only get one element


id is unique and cannot be repeated


Brother, you can use this to get the current,

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b35824-40528.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b35824-40528.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?