// 获取 input 元素的值
// 假设 HTML 中有一个 input 元素,如下:
// <input type="text" id="myInput" value="Hello World">
// 方法 1: 使用 document.getElementById()
var inputElement = document.getElementById("myInput");
var inputValue = inputElement.value;
console.log(inputValue); // 输出: Hello World
// 方法 2: 使用 document.querySelector()
var inputValue2 = document.querySelector("#myInput").value;
console.log(inputValue2); // 输出: Hello World
document.getElementById()
来获取具有指定 ID 的元素,然后通过 .value
属性获取该元素的值。document.querySelector()
来选择第一个匹配的元素(通过 CSS 选择器),同样可以通过 .value
属性获取其值。这两种方法都可以有效地获取 <input>
元素的值。
上一篇:js 获取当前地址
下一篇:js 日期比较
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站