[英]How we can call a public function inside public function using event handler in javascript
<div id="a"></div><br>
<div id="b"></div>
<script>
function Con(id){
this.id = id;
var me = this;
x = function(){ // public function
console.log(this.id)
}.bind(this);
this.y= function(){ // public function
console.log(me)
html = "<a href='javascript:void(0)' id='mycls-"+this.id+"' onclick='x()'>hello-"+this.id+"</a>";
document.getElementById(this.id).innerHTML = html;
}
this.y();
}
var v = new Con("a");
var v1 = new Con("b");
</script>
我有一个JavaScript构造函数,我想从y公共函数内部的链接中调用x公共函数。 我想获得正确的ID EX:如果我单击a div,它应该控制台a;或者如果我单击b div,它应该控制台b,但它总是只控制台b。
请像参数一样在x函数中传递ID: onclick='x("+this.id+")'
x = function(arg){ // public function
console.log(arg.id)
}.bind(this);
请找到总代码:
<div id="a"></div><br>
<div id="b"></div>
<script>
function Con(id){
this.id = id;
var me = this;
x = function(arg){ // public function
console.log(arg.id)
}.bind(this);
this.y= function(){ // public function
html = "<a href='javascript:void(0)' id='mycls-"+this.id+"' onclick='x("+this.id+")'>hello-"+this.id+"</a>";
document.getElementById(this.id).innerHTML = html;
}
this.y();
}
var v = new Con("a");
var v1 = new Con("b");
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.