使用 value 从文本框中提取值,并可以使用 innerHTML 在段落中显示。
以下是代码 -
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script xx_src="https://code.jquery.com/jquery-1.12.4.js"></script> <script xx_src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <body> Enter your Name: <input type="text" placeholder="enter your name" id="txtInputData"> <button onclick="displayName()">Click Me</button> <p id="show_name"> </p> </body> <script> function displayName() { var originalName = document.getElementById("txtInputData").value; document.getElementById("show_name").innerHTML = "你的名字是 :" + originalName; } </script> </html>
要运行上述程序,请保存文件名“anyName.html(index.html)”。右键单击文件并在 VSCode 编辑器中选择“使用 Live Server 打开”选项 -
输出结果
这将在控制台上产生以下输出 -
现在,在文本框中输入值并单击按钮 -
单击按钮后,输出如下 -