javascript - 如何在我的页面上用鼠标单击一个按钮,然后触发对同一页面上的链接的单击?
问题描述
我有一个学习管理 Wordpress 主题,它会根据您正在观看的视频自动生成指向上一个和下一个视频的链接(视频 2 将有一个指向视频 1 的上一个链接和视频描述中指向视频 3 的下一个链接)。
我想在我的自定义视频播放上添加上一个和下一个按钮,单击这些按钮会触发 WordPress 主题已经生成的上一个和下一个链接。
我怎么会有一个按钮,当点击时,点击同一页面上的链接?
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>HTML 5 VIDEO PLAYER</title>
<script src="main.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link href="font-awesome/css/all.css" rel="stylesheet">
<link href="style.css" rel="stylesheet">
</head>
<body>
<!-- Previous & Next Video Buttons -->
<div id="video-arrows">
<button type="button" id="previous-arrow"><a href="previous-video"></a><i class="fas fa-chevron-left"></i></button>
<button type="button" id="next-arrow"><a href="next-video"></a><i class="fas fa-chevron-right"></i></button>
</div>
<div class="random-text">
<h1>Video title</h1>
<h4><span id="view-count"></span> views</h4>
<p>Descriptions can convey valuable information that helps viewers find your videos in search results and understand what they’ll be watching. Well-written descriptions with the right keywords can boost views and watch time because they help your video show up in search results.</p>
<a href="https://www.icloud.com/" target="_blank" id="previous-video">Previous</a>
<a href="https://google.com" target="_blank" id="next-video">Next</a>
</div>
</body>
</html>
window.onload = function() {
var nextArrow = document.getElementById('next-arrow')
var prevArrow = document.getElementById('previous-arrow')
$(document).ready(function(){
$(nextArrow).on('click', function () {
$('#next-video').click();
});
$(prevArrow).on('click', function () {
$('#previous-video').click();
});
});
}
它没有给出任何错误它只是不起作用
解决方案
尝试$('#next-video')[0].click();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<button onclick="console.log('A')" id="A">A</button>
<button onclick="$('#A')[0].click()">B</button>
推荐阅读
- qt - qml中的滑块x和y位置
- javascript - 当变体的色样悬停在 Shopify 上时,如何显示变体图像?
- android - 在 ondestroy 中清除 CompositeDisposible?
- bash - 有没有更快的方法在另一个文件中查找一个文件的内容?
- c# - Unity Line Renderer 不会跟随光线投射?
- c# - 从另一个类引用一个类中的静态变量,错误用类型名称来限定它
- python - 如何在一个对象类型列中有空格时在python中读取csv文件
- r - 使用区间距离而不是单元距离连接来自不同组的异常区间
- scrum - 在 sprint 期间重新分配用户故事?
- c# - 为什么我们不能在 c# struct 的声明中初始化实例字段?