【问题标题】:Using jQuery in javascript class在 javascript 类中使用 jQuery
【发布时间】:2015-10-06 08:40:36
【问题描述】:

function count(x,y) {
  this.x;
  this.y;
  this.plus = function() {
    return this.x + this.y;
  }
  this.checkResult = function() {
    $("#test").click(function() {
      this.plus(); //help here
    });
  }
}
<p id='test'></p>

"this" 返回被点击的元素,那么如何在 jQuery 中运行方法呢?

【问题讨论】:

  • 我认为您需要学习 javascript,然后针对您遇到的问题提出问题。你可以从this开始。

标签: javascript jquery class object this


【解决方案1】:

试试这个:您可以将函数this 存储到某个变量并使用它。

function count(x,y) {
  this.x;
  this.y;
  this.plus = function() {
    return this.x + this.y;
  }
  this.checkResult = function() {
    var $this = this;
    $("#test").click(function() {
      alert($this.plus()); //help here
    });
  }
}
<p id='test'>Click me</p>

【讨论】:

    猜你喜欢
    • 2019-08-09
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    • 2017-11-21
    • 2013-02-27
    • 1970-01-01
    • 2017-07-31
    • 2010-10-21
    相关资源
    最近更新 更多