function fetchInputValue() {
const inputElement = document.querySelector('input[type="text"]');
if (inputElement) {
return inputElement.value;
}
return undefined; //如果找不到元素则返回undefined
}
//使用fetchInputValue()函数获取文本输入元素的值
const inputValue = fetchInputValue();
console.log(inputValue);
此代码使用querySelector()方法选择一个type属性为"text"的input元素,并返回其值。如果找不到该元素,则返回undefined。可以使用此函数获取任何一个文本输入框的值,只需更改选择器即可。