2 回答

TA貢獻1871條經驗 獲得超13個贊
你可以這樣做:
const submitFunc = () => {
const value = document.getElementById('name').value;
const parent = document.createElement('div');
const pTag = document.createElement('p');
pTag.innerText = value;
parent.appendChild(pTag);
document.body.appendChild(parent);
}
<button onclick="submitFunc()">Submit</button>
<input placeholder="Enter Name Here" id="name">

TA貢獻1858條經驗 獲得超8個贊
如果你想將其顯示為 H1 元素,你可以執行以下操作
const submitFunc = () => {
const value = document.getElementById('name').value;
const parent = document.createElement('div');
const hTag = document.createElement('h1');
var node = document.createTextNode(value);
hTag.appendChild(node);
parent.appendChild(pTag);
document.body.appendChild(parent);
}
這是要使用的標記。
<button onclick="submitFunc()">Submit</button>
<input placeholder="Enter Name Here" id="name">
- 2 回答
- 0 關注
- 145 瀏覽
添加回答
舉報