使用纯 JavaScript 获取 DOM 元素值

发布于 2024-10-02 10:51:21 字数 759 浏览 0 评论 0原文

这些解决方案之间有什么区别吗?

解决方案一:

function doSomething(id, value) {
  console.log(value);
  //...
}
<input id="theId" value="test" onclick="doSomething(this.id, this.value)" />

...以及解决方案 2:

function doSomething(id) {
  var value = document.getElementById(id).value;
  console.log(value);
  //...
}
<input id="theId" value="test" onclick="doSomething(this.id)" />

Is there any difference between these solutions?

Solution 1:

function doSomething(id, value) {
  console.log(value);
  //...
}
<input id="theId" value="test" onclick="doSomething(this.id, this.value)" />

...and Solution 2:

function doSomething(id) {
  var value = document.getElementById(id).value;
  console.log(value);
  //...
}
<input id="theId" value="test" onclick="doSomething(this.id)" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

此岸叶落 2024-10-09 10:51:21

更新:问题已被编辑。现在这两种解决方案是等效的。

原始答案

,最值得注意的是!我认为第二个不会起作用(如果可以的话,也不是很便携)。第一个应该没问题。

// HTML:
<input id="theId" value="test" onclick="doSomething(this)" />

// JavaScript:
function(elem){
    var value = elem.value;
    var id    = elem.id;
    ...
}

这也应该有效。

Update: The question was edited. Both of the solutions are now equivalent.

Original answer

Yes, most notably! I don't think the second one will work (and if it does, not very portably). The first one should be OK.

// HTML:
<input id="theId" value="test" onclick="doSomething(this)" />

// JavaScript:
function(elem){
    var value = elem.value;
    var id    = elem.id;
    ...
}

This should also work.

晨光如昨 2024-10-09 10:51:21

第二个功能应该有:

var value = document.getElementById(id).value;

那么它们基本上是相同的功能。

The second function should have:

var value = document.getElementById(id).value;

Then they are basically the same function.

苹果你个爱泡泡 2024-10-09 10:51:21

在第二个版本中,您传递从 this.id 返回的字符串。不是元素本身。

所以 id.value 不会给你你想要的。

您需要使用 this 传递元素。

doSomething(this)

然后:

function(el){
    var value = el.value;
    ...
}

注意:在某些浏览器中,如果您这样做,第二个也可以工作:

window[id].value 

因为元素 ID 是全局属性,但是这并不安全

最有意义的做法是仅使用 this 传递元素,而不是使用其 ID 再次获取它。

In the second version, you're passing the String returned from this.id. Not the element itself.

So id.value won't give you what you want.

You would need to pass the element with this.

doSomething(this)

then:

function(el){
    var value = el.value;
    ...
}

Note: In some browsers, the second one would work if you did:

window[id].value 

because element IDs are a global property, but this is not safe.

It makes the most sense to just pass the element with this instead of fetching it again with its ID.

廻憶裏菂餘溫 2024-10-09 10:51:21

传递对象:

doSomething(this)

您可以从对象获取所有数据:

function(obj){
    var value = obj.value;
    var id = obj.id;
}

或仅传递id

doSomething(this.id)

获取对象及其后的值:

function(id){
    var value = document.getElementById(id).value;  
}

Pass the object:

doSomething(this)

You can get all data from object:

function(obj){
    var value = obj.value;
    var id = obj.id;
}

Or pass the id only:

doSomething(this.id)

Get the object and after that value:

function(id){
    var value = document.getElementById(id).value;  
}
征﹌骨岁月お 2024-10-09 10:51:21

如果我们看效果的话,没有什么区别——价值是一样的。然而还有更多...

解决方案 3:

function doSomething() {
  console.log( theId.value );
}
<input id="theId" value="test" onclick="doSomething()" />

如果DOM元素有id那么你可以直接在js中使用它

There is no difference if we look on effect - value will be the same. However there is something more...

Solution 3:

function doSomething() {
  console.log( theId.value );
}
<input id="theId" value="test" onclick="doSomething()" />

if DOM element has id then you can use it in js directly

叹沉浮 2024-10-09 10:51:21

这也应该有效。

function doSomething() {
  yourElement = document.getElementById("yourID);
    yourValue = yourElement.value; console.log(yourValue);
    console.log(yourValue);
  }
<div id="yourID" value="1" onclick="doSomething()">
</div>

function doSomething() {
  console.log( theId.value );
}
<input id="theId" value="test" onclick="doSomething()" />

This should also work.

function doSomething() {
  yourElement = document.getElementById("yourID);
    yourValue = yourElement.value; console.log(yourValue);
    console.log(yourValue);
  }
<div id="yourID" value="1" onclick="doSomething()">
</div>

function doSomething() {
  console.log( theId.value );
}
<input id="theId" value="test" onclick="doSomething()" />

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文