
本文介绍在单页视频播放器中,如何根据当前播放视频的标签,从本地视频数据中筛选出具有相同标签的其他视频,并动态渲染为缩略图+标题的推荐列表。
本文介绍在单页视频播放器中,如何根据当前播放视频的标签,从本地视频数据中筛选出具有相同标签的其他视频,并动态渲染为缩略图+标题的推荐列表。
要在视频播放页下方精准展示“相似视频”,关键在于:以当前视频的标签为基准,检索其余视频中至少包含一个共同标签的条目,并以结构化 HTML 动态渲染。以下为完整实现方案:
✅ 步骤一:补全基础 HTML 结构
确保页面中存在用于承载推荐视频的容器(注意类名需与 JS 一致):
<div class="other-vids" id="similar-videos-section"> <!-- 相似视频将自动插入此处 --> </div>
✅ 步骤二:修正并增强 JavaScript 逻辑
您原代码中存在几处关键问题需修复:
-
loadVideoPage()被错误地递归调用(会导致无限循环); -
video2对象缺少逗号导致语法错误; - 缺少对
tags字段缺失情况的容错处理(如某视频未定义tags数组)。
以下是优化后的完整 loadVideoPage 函数(含相似视频逻辑):
const allVideos = [
{
id: "video1",
title: "Video 1",
preview: "video1-preview.jpg",
url: "https://example.com/video1.mp4",
tags: ["action", "adventure"]
},
{
id: "video2",
title: "Video 2",
preview: "video2-preview.jpg",
url: "https://example.com/video2.mp4",
tags: ["thriller", "mystery"]
},
{
id: "video3",
title: "Video 3",
preview: "video3-preview.jpg",
url: "https://example.com/video3.mp4",
tags: ["action", "sci-fi"] // 与 video1 共享 'action' → 将被推荐
}
];
function getUrlParameter(name) {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get(name);
}
function loadVideoPage() {
const videoId = getUrlParameter('videoId');
const mainVideo = allVideos.find(video => video.id === videoId);
if (!mainVideo) {
console.error("Video not found:", videoId);
return;
}
// ▶️ 加载主视频
const mainVideoElement = document.getElementById('main-vid');
if (mainVideoElement) mainVideoElement.src = mainVideo.url;
// ▶️ 设置标题
const videoTitleElement = document.querySelector('.vid-name');
if (videoTitleElement) videoTitleElement.textContent = mainVideo.title;
// ▶️ 安全设置标签(兼容无 tags 字段的视频)
const tagsElement = document.getElementById('tags');
if (tagsElement) {
const tagList = Array.isArray(mainVideo.tags) ? mainVideo.tags : [];
tagsElement.dataset.tags = tagList.join(',');
}
// ▶️ ? 生成相似视频推荐(核心逻辑)
const otherVideoContainer = document.querySelector(".other-vids");
if (!otherVideoContainer) return;
// 筛选:排除自身 + 至少有一个共同标签
const otherVidsWithMatchingTag = allVideos.filter(vid => {
if (vid.id === mainVideo.id) return false;
const vidTags = Array.isArray(vid.tags) ? vid.tags : [];
return mainVideo.tags.some(tag => vidTags.includes(tag));
});
// 清空旧内容(可选,避免重复追加)
otherVideoContainer.innerHTML = '';
// 渲染每个相似视频
otherVidsWithMatchingTag.forEach(vid => {
const otherVid = document.createElement("div");
otherVid.classList.add("suggest-video");
const img = document.createElement("img");
img.src = vid.preview;
img.alt = vid.title; // 可访问性最佳实践
img.loading = "lazy"; // 懒加载提升性能
const title = document.createElement("p");
title.textContent = vid.title;
otherVid.append(img, title);
otherVideoContainer.appendChild(otherVid);
});
}
// ✅ 页面加载完成后执行(避免 DOM 未就绪)
document.addEventListener('DOMContentLoaded', loadVideoPage);⚠️ 注意事项与进阶建议
-
标签匹配强度:当前逻辑为“至少一个标签匹配”,如需更精准(如按匹配数量排序),可改用
map().sort()计算重合度; -
性能优化:若视频库较大(>100 条),建议预建标签索引映射表(
{ action: ['video1','video3'], ... }),避免每次遍历全量数组; -
样式控制:为
.suggest-video添加 CSS(如display: inline-block; width: 200px; margin: 8px;)实现网格布局; -
兜底提示:当无匹配视频时,可在
.other-vids内插入<p>No similar videos found.</p>提升用户体验。
通过以上实现,您即可在任意视频详情页中,全自动、零配置地呈现语义相关的视频推荐,显著提升用户停留时长与内容发现效率。

















