javascript - Jquery使用on绑定的事件函数中使用构造函数的原型方法
本文介绍了javascript - Jquery使用on绑定的事件函数中使用构造函数的原型方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
var Obj = function( target ) {
this.target = $(target);
this.num = 0;
this.setNum = setNum;
}
Obj.prototype.display = function() {
alert( this.num );
}
function setNum() {
var $t = this.target;
$t.on('click', 'a', function(e) {
使this.num的值为a的value;
这里执行原型函数display;
});
}
应该怎么写?
解决方案
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>demo</title>
</head>
<body>
<div class="haha">哈哈</div>
</body>
</html>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script>
var Obj = function( target ) {
this.target = $(target);
this.num = 0;
this.setNum = setNum;
}
Obj.prototype.display = function() {
alert( this.num );
}
function setNum() {
var $t = this.target;
var _this=this;
console.log($t);
$t.on('click',{a:"hihihihi"},function(e) {
// 使this.num的值为a的value;
// 这里执行原型函数display;
console.log(e);
_this.num=e.data.a;
_this.display();
console.log(_this);
});
}
var dom=new Obj(".haha");
console.log(dom);
dom.setNum();
</script>
这篇关于javascript - Jquery使用on绑定的事件函数中使用构造函数的原型方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文