【发布时间】:2017-03-06 19:17:29
【问题描述】:
可以在事件 onclik 中添加方法。此代码在 html "onclick='alert("TEST");undefined'" 中创建事件 onclick。我想得到 "onclick='alert("TEST");stdaction('t1')'" 其中 stdaction('t1') 是 Button 类中的函数
function Button(id,onclick){
this.id = id;
this.onclick= onclick;
}
Button.prototype.create = function(){
var button = $('<div>');
button.attr('id',this.id);
button.html('Default');
button.attr('onclick',this.onclick+';'+this.stdaction(this.id)); // this is problem
return button.prop('outerHTML');
}
Button.prototype.stdaction = function(id){
$('#'+id).addClass('std-active');
}
var oneButton = new Button('t1','alert("TEST")');
$('#newButton').append(oneButton.create());
我按照建议进行了更改,Paflow 更好,但我想创建一个连接运行 onclick 的函数,甚至有一个单独的方法 https://jsfiddle.net/uevckbe0/
【问题讨论】:
-
用引号将
this.stdaction(this.id)括起来。
标签: javascript jquery object