js中this的指向

短命女 2022-09-14 09:12 334阅读 0赞
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>click函数的使用</title>
  8. </head>
  9. <body>
  10. <button onclick="dj()" id="anniu">你好世界</button>
  11. <button id="haha" onclick="haha()">haha</button>
  12. <!-- 从这里看出可以是windows触发的 -->
  13. <input type="file" onchange="loadImageFile()" id="upload" />
  14. <script>
  15. function dj() {
  16. // console.log("nihaoshijie");
  17. // document.getElementById("haha").click();
  18. // 可能这是一个动作所以输出的是未定义
  19. // console.log(document.getElementById("haha").click());
  20. filenamedz = document.getElementById("upload").click(); //也有可能是它触发的
  21. filename = document.getElementById("upload");
  22. console.log(filename);
  23. }
  24. function loadImageFile() {
  25. console.log(this); //确实是windows,以后不知道是就输出一下
  26. //这里的this应该是指向的是那个按钮,假如我这里写this指的是windows调用
  27. var file = filename.files;
  28. console.log(file);
  29. }
  30. function haha() {
  31. console.log("lj");
  32. }
  33. </script>
  34. </body>
  35. </html>

发表评论

表情:
评论列表 (有 0 条评论,334人围观)

还没有评论,来说两句吧...

相关阅读

    相关 jsthis指向理解

    个人理解为以下几点:(仅供参考) 1、普通函数的 this 指向的是函数执行时的 调用对象,如函数执行时未指明调用对象,则 this 指向 window 2、箭头函数的 t

    相关 JSthis指向

    this指向: 与我们常见的很多语言不同,JavaScript 函数中的 `this` 指向并不是在函数定义的时候确定的,而是在调用的时候确定的。换句话说,函数的调用方式

    相关 jsthis指向

    首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,实际上this的最终指向的是那个调用它的对象。 为什么要学习t