javascript - 在 html 页面的不同位置显示来自输入字段的文本
问题描述
我想要实现的是在输入字段中输入文本,并在提交时希望文本出现在 HTML 页面的不同位置。
示例:我有一个包含以下行的页面
红车-绿车-
黄
车-
在输入字段中输入 TEXT 后,我希望将行更改为
redcar-TEXT greencar-TEXT Yellowcar
-
TEXT
这可能实现吗?如果您有示例代码,将非常受欢迎。
这对我有用
function myFunction()
{
var x;
var person=prompt("Please enter your text","text");
if (person!=null)
{
x="redcar-" + person + "";
document.getElementById("demo").innerHTML=x;
}
}
<html>
<body>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
</body>
</html>
但我无法让它适用于多行
解决方案
您可以这样做,只需将您想要的所有“汽车”添加到cars
数组中
function myFunction() {
const person = prompt("Please enter your text","text");
const cars = ['redcar-', 'yellowcar-'];
if (person != null) {
document.getElementById("demo").innerHTML = cars.map(c => c + person).join('<br>');
}
}
<html>
<body>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
</body>
</html>
推荐阅读
- r - 为什么我不能将变异结果保存在 r 中?
- amazon-web-services - 如何共享 AWS AppSync GraphQL 架构?
- python - 如何在列表 python 中加入字符串?
- c++ - Remove duplication in equivalent const and non-const members when returning optional reference by value
- git - 将 git repo 的快照复制到另一个没有历史记录的 repo
- c++ - 设置基类等于子类时如何使用子函数?
- microsoft-graph-api - MS Graph API 是否支持身份保护策略?
- wordpress - Wordpress Timber 插件自定义路由抛出 404
- c - 为什么 fprintf 给出分段错误?
- android - 从android studio中的firebase检索数据