如何从另一个html文件中提取具有特定类名的前10个元素并将其显示在我的frontpage上?

vlf7wbxs  于 4个月前  发布在  其他
关注(0)|答案(1)|浏览(38)

**已关闭。**此问题需要debugging details。目前不接受回答。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答问题。
9天前关闭
Improve this question
我在一个html文件(称为“movies.html”)中有一个电影列表,看起来像这样:

<div class="movie">Content 1</div>
<div class="movie">Content 2</div>
<div class="movie">Content 3</div>
...

字符串
我想在我的frontpage(“index.html”)中拉取并显示前10个类名为“movie”的div,这样每当我向“movies.html”中的列表添加另一部电影时,我的frontpage就会自动更新。
我如何才能做到这一点?

tmb3ates

tmb3ates1#

使用querySelectorAll获取所有div,然后使用slice获取前n个元素:

const AllMovies = document.querySelectorAll(".movie");  
var top10 = [...AllMovies].slice(0, 10);

字符串

相关问题