gpt4 book ai didi

javascript - document.getElementById ('url_input' ).value 不打印该值

转载 作者:行者123 更新时间:2023-12-01 03:00:34 25 4
gpt4 key购买 nike

我试图用以下代码获取文本输入的值:

var input = document.getElementById('url_input').value;

document.getElementById("send_url").onclick = function(){
console.log(input);
}

这不起作用,但如果我像这样改变:

var input = document.getElementById('url_input');
document.getElementById("send_url").onclick = function(){
console.log(input.value);
}

这两者有什么不同?为什么第一个不起作用?

最佳答案

what's the different between this two?

在第一个示例中,您将值复制输入中:

var input = document.getElementById('url_input').value;

然后重复重新记录该值:

document.getElementById("send_url").onclick = function(){
console.log(input);
}

将值复制到 input 中不会在 inputHTMLInputElementvalue 之间创建任何类型的持续链接 属性。它只是将 value 的值复制到 input 中。

在第二个示例中,您每次都会从 HTMLInputElement 获取值:

var input = document.getElementById('url_input');
document.getElementById("send_url").onclick = function(){
console.log(input.value);
}

每次点击时,您都会向 HTMLInputElement 询问其当前状态。请注意,input 中的值在两次点击之间仍然不会改变;它是对 url_input 元素的引用,该元素是一个对象,并且该引用在此代码中不会更改。改变的是对象(HTMLInputElement)的状态,并且您每次都要求它提供其当前状态。

关于javascript - document.getElementById ('url_input' ).value 不打印该值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46450857/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com