我想使用 html 中用户输入字段中的文本在 javascript 中构建一个数组,并将其显示在带有 .push 的 p 标记内。如何?
I want to build an array in javascript using text from a user input field in html and have it appear inside a p tag with .push. HOW?
这是我的HTMLSheet--
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>repl.it</title>
<link href="index.css" rel="stylesheet" type="text/css" />
</head>
<body>
<label for= "like">Name Choices</label><br>
<input type="text" id="like" placeholder= "Enter a name selection">
<button>Add to list</button>
<p id="nameList"></p>
<script src="index.js"></script>
</body>
</html>
--------------------------------
And this is my javascript code/sheet:
const inputs = document.querySelectorAll('input');
const btn = document.querySelector('button');
const para = document.querySelectorAll('p');
let bNames = document.getElementById('like');
const arr = [bNames];
btn.addEventListener('click', function(){
let bNames = inputs.value;
let result = `arr.push[bNames]`;
p.push.innerHTML = ("like");
return result;
})
-----
我可以输入名称,但是当我单击提交时,文本消失了,我只剩下一个打开的 space 和无数组。我做错了什么?
解法:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>INject</title>
</head>
<body>
<label for="like">Name Choices</label><br />
<input type="text" id="like" placeholder="Enter a name selection" />
<button onclick="enterParagraph()">Add to list</button>
<p id="nameList"></p>
</body>
</html>
<script>
const arr = [];
function enterParagraph() {
const field = document.querySelector("#like").value;
arr.push(field);
document.querySelector("#nameList").innerHTML = arr;
}
</script>
从你的代码我可以猜到你对javascript了解不多。我建议您先多了解一下 javascript。 MDN 有一个很好的 JavaScript 指南。
https://developer.mozilla.org/en-US/docs/Learn/JavaScript
这是我的HTMLSheet--
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>repl.it</title>
<link href="index.css" rel="stylesheet" type="text/css" />
</head>
<body>
<label for= "like">Name Choices</label><br>
<input type="text" id="like" placeholder= "Enter a name selection">
<button>Add to list</button>
<p id="nameList"></p>
<script src="index.js"></script>
</body>
</html>
--------------------------------
And this is my javascript code/sheet:
const inputs = document.querySelectorAll('input');
const btn = document.querySelector('button');
const para = document.querySelectorAll('p');
let bNames = document.getElementById('like');
const arr = [bNames];
btn.addEventListener('click', function(){
let bNames = inputs.value;
let result = `arr.push[bNames]`;
p.push.innerHTML = ("like");
return result;
})
-----
我可以输入名称,但是当我单击提交时,文本消失了,我只剩下一个打开的 space 和无数组。我做错了什么?
解法:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>INject</title>
</head>
<body>
<label for="like">Name Choices</label><br />
<input type="text" id="like" placeholder="Enter a name selection" />
<button onclick="enterParagraph()">Add to list</button>
<p id="nameList"></p>
</body>
</html>
<script>
const arr = [];
function enterParagraph() {
const field = document.querySelector("#like").value;
arr.push(field);
document.querySelector("#nameList").innerHTML = arr;
}
</script>
从你的代码我可以猜到你对javascript了解不多。我建议您先多了解一下 javascript。 MDN 有一个很好的 JavaScript 指南。 https://developer.mozilla.org/en-US/docs/Learn/JavaScript