简体   繁体   English

单击指向类中函数的事件附加事件处理程序

[英]Attaching event handler on click pointing to function in class

Is it possible to attach an event handler on click pointing to function in class? 是否可以在指向类中函数的单击上附加事件处理程序? Like this: 像这样:

class SomeClass {
  render() {
    this.parentElement.append(`
      <form>
        <input type="text" onkeydown="${this.keydown()}"/>
        <button onclick="this.onclick();">Submit</button>
        <span></span>
      </form>
    `);
  }

  onkeydown() {}
  onclick() {}
}

Thank you. 谢谢。

The way you've done it here? 您在这里完成的方式? No. Especially since append will only append nodes, not create nodes. 不会。尤其是因为append仅将追加节点,而不创建节点。 Text is a node, but you won't see your form, just the HTML that would produce a form. 文本一个节点,但是您看不到表单,只是会生成表单的HTML。 However, even if you use innerHTML instead, it still won't work. 但是,即使您使用innerHTML代替,它仍然无法工作。

For the first function, you're not putting a reference to onkeypress , you're literally calling the function when you call render . 对于第一个函数,您没有引用onkeypress ,而是在调用render时实际上是在调用该函数。 If you remove the parenthesis you just wind calling the default value of the function. 如果删除括号,则只需调用该函数的默认值即可。 The same as if you put onkeypress.toString() . 与放置onkeypress.toString() Not very helpful. 不是很有帮助。

The second function, onclick is a bit trickier. 第二个功能onclick有点棘手。 It does call a function, just not the one you were hoping for. 确实调用了一个函数,而不是您希望的那个函数。 Understanding the this keyword in JavaScript is tricky. 在JavaScript中理解this关键字非常棘手。 In this case, even though you're writing the html in a class, when the button is clicked the context of this is not that class. 在这种情况下,即使你在一个类中,当按钮被点击的情况下编写HTML this是不是类。 Instead it's the element that has the onclick listener attached, button . 而是连接了onclick侦听器button的元素。 So what function does it call? 那么它调用什么功能呢? Itself. 本身。 Over and over again. 一遍又一遍地。 This is a very good example of why it's not a good idea to name functions the same as native ones. 这是一个很好的例子,说明为什么将函数命名为与本机函数相同并不是一个好主意。 Sadly though, even if you change it's name to something safer, like handleClick , it still doesn't work. 遗憾的是,即使您将其名称更改为更安全的名称(如handleClick ,它也仍然无法正常工作。 The button (the current value of this ) just doesn't have the function you're trying to reference. buttonthis的当前值)只是没有您要引用的功能。

So, is it possible? 那么,有可能吗? Depends on what you're looking for. 取决于您要寻找的东西。 If you render the node and then add an event listener in the more traditional ways, sure. 如果渲染节点,然后以更传统的方式添加事件侦听器,请确保。 Just add this.parentElement.querySelector('button').onclick = this.handleClick; 只需添加this.parentElement.querySelector('button').onclick = this.handleClick; or this.parentElement.querySelector('input').addEventListener('keydown', this.handleKeyDown) after you add the form to the dom (using innerHTML or insertAdjacentHTML , instead of append ). 或将表单添加到dom后(使用innerHTMLinsertAdjacentHTML ,而不是append ),或this.parentElement.querySelector('input').addEventListener('keydown', this.handleKeyDown) )。 Or follow Ibrahim's sugestion and create the elements using createElement and attaching the event handlers before you append them to the parent element. 或按照易卜拉欣的建议 ,使用createElement创建元素并附加事件处理程序,然后再将它们附加到父元素。 Though, if you want to have access to your classes this , you might want to bind them first. 但是,如果您想访问this的类,则可能要先绑定它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM