javascript - 在使用稍后提到的原型(prototype)的类中设置事件?

标签 javascript jquery class jquery-events

当我到达 console.log() 它的返回窗口时。

如何获取考虑中的元素?

let builder = function(){
    this.box = $(`<div id="box" class="box">`);
    this.inputcontainer = $(`<div id="inputcontainer" class="inputcontainer">`).appendTo(this.box);
    this.textarea = $(`<textarea id="textarea"></textarea>`).appendTo(this.inputcontainer);
    this.textarea.on("change keyup keydown input paste", this.postmsg);
    this.chat.appendTo($('body'));
}

//someothercode

builder.prototype.postmsg = (e) => {
    console.log(this); // returns window when I need it to be referencing textarea
}

let instance = new builder();

最佳答案

builder.prototype.postmsg = function(e){
    console.log(this.textarea);
}

关于javascript - 在使用稍后提到的原型(prototype)的类中设置事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47495634/

相关文章:

javascript - 从数组中添加按钮的值

javascript - Bootstrap - 在多个输入上预先输入

javascript - 获取li内子标签的html内容

java - 基于类的类变量 - 不清楚

c++ - 如何使用在函数中本地创建的对象而不将其复制到内存中?

javascript - 使用 JavaScript 拖放

Javascript 包括给出意想不到的结果

javascript - 传单R,如何使与 child 统计相关的集群图标出现?

jquery - ASP.NET 像 ValidationGroup 使用 JQuery 验证插件?

python - Python 中的嵌套类