““此”关键字在HTML ELEMENTS行为内的Javascript中[重复]

问题描述 投票:0回答:1

此问题已经在这里有了答案:

th,td {
  padding: 5px;
}
</style>
<body>

<h2>The XMLHttpRequest Object</h2>

<form action=""> 
  <select name="customers" onchange="showCustomer(this.value)">
    <option value="">Select a customer:</option>
    <option value="ALFKI">Alfreds Futterkiste</option>
    <option value="NORTS ">North/South</option>
    <option value="WOLZA">Wolski Zajazd</option>
  </select>
</form>
<br>
<div id="txtHint">Customer info will be listed here...</div>

<script>
function showCustomer(str) {
  var xhttp;  
  if (str == "") {
    document.getElementById("txtHint").innerHTML = "";
    return;
  }
  xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("txtHint").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "getcustomer.asp?q="+str, true);
  xhttp.send();
}
</script>
</body>
</html>

[抱歉,很抱歉,这个基本问题:)仅研究了一些JavaScript的AJAX,我对onchange EventHandler中此代码块中对“ this”的使用感到困惑。似乎“ this”指的是选项元素,但我真的不明白如何或为什么。

编辑:似乎不是那么基本。

我阅读了有关“ this”的一般问题的详细答案

How does the "this" keyword work?

还有很棒的文章:

http://www.digital-web.com/articles/scope_in_javascript/

他们俩都为很多人服务,但并没有完全解决我的问题。

我无法通过JavaScript函数在HTML元素中使用“ this”时,确切的行为是什么?

我希望有人能理解我的意思

javascript html ajax dom
1个回答
-1
投票

这是指它所属的对象,例如

© www.soinside.com 2019 - 2024. All rights reserved.