我如何通过控制台触发没有 id 的 link

How do i trigger a link with no id via console

我只是想通过控制台触发一个没有 id 的 link。我尝试了多种方案,none 似乎有效。这是一个 link,有很多 类,但没有 ID。

$('.videoHolder').find('a.icon-play').trigger('click');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="videoHolder hover">
  <div class="videoDisplay">

   <video class="persistentNativePlayer nativeEmbedPlayerPid" poster="data:image/png,%89PNG%0D%0A%1A%0A%00%00%00%0DIHDR%00%00%00%01%00%00%00%01%08%02%00%00%00%90wS%DE%00%00%00%01sRGB%00%AE%CE%1C%E9%00%00%00%09pHYs%00%00%0B%13%00%00%0B%13%01%00%9A%9C%18%00%00%00%07tIME%07%DB%0B%0A%17%041%80%9B%E7%F2%00%00%00%19tEXtComment%00Created%20with%20GIMPW%81%0E%17%00%00%00%0CIDAT%08%D7c%60%60%60%00%00%00%04%00%01'4'%0A%00%00%00%00IEND%AEB%60%82"
      id="pid_kaltura_player" kentryid="1_4u0ocu4u" kuiconfid="" kwidgetid="" kpartnerid="" preload="none" width="560" height="395" src="" style="position: absolute; left: 0px; top: 0px;"></video>
    <div class="videoShadow"></div>
    <div class="mwEmbedPlayer" id="kaltura_player" style=""></div>
  </div><a tabindex="-1" href="#" role="button" class="comp largePlayBtn  largePlayBtnBorder icon-play" aria-label="Play clip" data-order="1" data-plugin-name="largePlayBtn" style="display: flex; opacity: 1;">Mylink</a></div>

您可以定位 <a> 标签并触发 .click()

为了演示这一点,我制作了一个 test(),如果单击 link,它将在控制台中打印 MyLink Clicked....

function test() {
  console.log("MyLink Clicked....");
}

document.getElementsByTagName("a")[0].click();
<div class="videoHolder hover"><div class="videoDisplay"> 

    <video class="persistentNativePlayer nativeEmbedPlayerPid" poster="data:image/png,%89PNG%0D%0A%1A%0A%00%00%00%0DIHDR%00%00%00%01%00%00%00%01%08%02%00%00%00%90wS%DE%00%00%00%01sRGB%00%AE%CE%1C%E9%00%00%00%09pHYs%00%00%0B%13%00%00%0B%13%01%00%9A%9C%18%00%00%00%07tIME%07%DB%0B%0A%17%041%80%9B%E7%F2%00%00%00%19tEXtComment%00Created%20with%20GIMPW%81%0E%17%00%00%00%0CIDAT%08%D7c%60%60%60%00%00%00%04%00%01'4'%0A%00%00%00%00IEND%AEB%60%82" id="pid_kaltura_player" kentryid="1_4u0ocu4u" kuiconfid="" kwidgetid="" kpartnerid="" preload="none" width="560" height="395" src="" style="position: absolute; left: 0px; top: 0px;"></video><div class="videoShadow"></div><div class="mwEmbedPlayer" id="kaltura_player" style=""></div>
</div><a tabindex="-1" href="#" role="button" class="comp largePlayBtn  largePlayBtnBorder icon-play" aria-label="Play clip" data-order="1" data-plugin-name="largePlayBtn" onclick="test()" style="display: flex; opacity: 1;">Mylink</a></div>

我理解你的麻烦,link你不能自己点击它,你只能添加一些点击功能,然后触发它,你可以用这个代码试试:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<div class="videoHolder hover">
    <div class="videoDisplay">

        <video class="persistentNativePlayer nativeEmbedPlayerPid"
               poster="data:image/png,%89PNG%0D%0A%1A%0A%00%00%00%0DIHDR%00%00%00%01%00%00%00%01%08%02%00%00%00%90wS%DE%00%00%00%01sRGB%00%AE%CE%1C%E9%00%00%00%09pHYs%00%00%0B%13%00%00%0B%13%01%00%9A%9C%18%00%00%00%07tIME%07%DB%0B%0A%17%041%80%9B%E7%F2%00%00%00%19tEXtComment%00Created%20with%20GIMPW%81%0E%17%00%00%00%0CIDAT%08%D7c%60%60%60%00%00%00%04%00%01'4'%0A%00%00%00%00IEND%AEB%60%82"
               id="pid_kaltura_player" kentryid="1_4u0ocu4u" kuiconfid="" kwidgetid="" kpartnerid="" preload="none"
               width="560" height="395" src="" style="position: absolute; left: 0px; top: 0px;"></video>
        <div class="videoShadow"></div>
        <div class="mwEmbedPlayer" id="kaltura_player" style=""></div>
    </div>
    <a tabindex="-1" href="#" role="button" class="comp largePlayBtn  largePlayBtnBorder icon-play"
       aria-label="Play clip" data-order="1" data-plugin-name="largePlayBtn"
       style="display: flex; opacity: 1;">Mylink</a></div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
    $('.videoHolder').find('a.icon-play').on('click',function (e){
        e.preventDefault();
        console.log('click');
    });
    $('.videoHolder').find('a.icon-play').click();

</script>
</body>
</html>