javascript - 如何在html中多次重复相同的图像?
问题描述
我想在整个页面宽度上重复一个图像-“box1”。我尝试了以下方法,但它不起作用。
var count = $(window).width() / $("#box1").width();
for (var i = 1; i <= count; i++) {
var paragraph = document.getElementById("top-section");
paragraph.innerHTML += "<img src='images/box1.png' id='box1'html>";
}
#top-section {
float: left;
}
<div id="top-section"></div>
解决方案
这个对我来说很好用。一探究竟...
只需克隆您需要的元素并将其附加到该部分中。
var count = $(window).width() / $(".box1").last().width();
for (var i = 1; i <= count; i++) {
$("#top-section").append($(".box1").last().clone(true));
}
#top-section {
float: left;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="top-section">
<img src="images/box1.png" class="box1">
</div>
推荐阅读
- linux - Shell 脚本查找 .git 文件夹,然后运行 git branch 命令
- flutter - 如何更新 FutureProvider 中的值?
- c# - 在 IOS Xamarin 中评估 javaScrip
- angular - 如何修复 RangeError:将文档添加到 Firebase Firestore 时超出了最大调用堆栈大小
- python-3.x - 是否有 Python 语言功能可以使用不同(但已知)的参数组合调用函数?
- javascript - How to merge/call the results of 2 callback function into a new function javascript?
- c# - C# 函数指针/委托作为类字段
- r - 将波斯日期转换为公历日期(也逆转换)
- postgresql - 将外部 SQL 数据库连接到 orion 上下文代理
- javascript - 带参数调用 JSF 方法 | JavaScript