javascript - 将 this 关键字与 selectedIndex 一起使用的优点?

标签 javascript this selectedindex

这样做有什么好处吗:

document.getElementById('mySelectBox').options[this.selectedIndex].value

在此:

document.getElementById('mySelectBox').options[selectedIndex].value

只是好奇。谢谢!

最佳答案

是的,如果您不使用this,您将访问变量而不是对象中的属性。

Javascript 不是面向对象的,因此您永远不会处于对象的范围内,您可以在不指定属性属于哪个对象的情况下访问属性。

无论如何,如果 this 引用该元素,只需使用:

this.options[this.selectedIndex].value

关于javascript - 将 this 关键字与 selectedIndex 一起使用的优点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11533750/

相关文章:

c# - selecteditems 和 selectedindex c#

jquery - 如何在 jQuery 中引用元素

c# - 在 ListView 中选择下一项

javascript - 如何根据javascript中的条件渲染模板

javascript - Aurelia 以及导入和使用 Highcharts X-range

javascript - 我可以选择类型 ="file"吗?

Javascript HTML 下拉验证

javascript - 从成员函数访问函数成员变量

java - addActionListener 和 "this"

c# - 选择一个项目两次或更多次