[英]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>
I have javascript constructor i want to call the x public function from a link which is inside of the y public function. 我有一个JavaScript构造函数,我想从y公共函数内部的链接中调用x公共函数。 I want to get the proper id EX: if I am click on the a div it should console a or if I am click on the b div it should console b but it always console only b.
我想获得正确的ID EX:如果我单击a div,它应该控制台a;或者如果我单击b div,它应该控制台b,但它总是只控制台b。
Please pass the ID in the x function like a argument: onclick='x("+this.id+")'
请像参数一样在x函数中传递ID:
onclick='x("+this.id+")'
x = function(arg){ // public function
console.log(arg.id)
}.bind(this);
Please find the total code: 请找到总代码:
<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.