要实现使用for
循环在表单之间添加<hr>
分隔符,但不在开头或结尾添加,可以通过以下几种方法来实现:
在每次循环中,判断当前是否为第一个或最后一个元素,如果不是,则添加<hr>
。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Separator</title>
</head>
<body>
<div id="form-container">
<!-- 假设有一个表单数组 -->
const forms = ['Form 1', 'Form 2', 'Form 3'];
for (let i = 0; i < forms.length; i++) {
document.write(`<div>${forms[i]}</div>`);
if (i < forms.length - 1) {
document.write('<hr>');
}
}
</div>
</body>
</html>
使用数组的join
方法来拼接表单和分隔符。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Separator</title>
</head>
<body>
<div id="form-container">
const forms = ['Form 1', 'Form 2', 'Form 3'];
const separator = '<hr>';
const formElements = forms.map(form => `<div>${form}</div>`).join(separator);
document.write(formElements);
</div>
</body>
</html>
使用模板字符串来动态生成HTML内容。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Separator</title>
</head>
<body>
<div id="form-container">
const forms = ['Form 1', 'Form 2', 'Form 3'];
let htmlContent = '';
forms.forEach((form, index) => {
htmlContent += `<div>${form}</div>`;
if (index < forms.length - 1) {
htmlContent += '<hr>';
}
});
document.write(htmlContent);
</div>
</body>
</html>
<hr>
。map
方法生成每个表单的HTML,然后使用join
方法将它们用<hr>
连接起来。<hr>
。这种方法适用于需要在多个表单之间添加分隔符的任何场景,例如在一个页面上展示多个用户填写的表单,或者在网页的不同部分之间进行视觉分隔。
通过以上方法,可以有效地在表单之间添加分隔符,同时避免在开头或结尾出现多余的分隔符。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云