我需要在元素中创建一个链接,然后用函数绑定它。在消息面板的示例关闭按钮。onclick - 匿名函数vs定义vs html onclick
哪种解决方案最好,为什么?我的意思是性能/标准。或者,也许还有别的一些东西,甚至更好:
1. 定义函数+的onclick函数调用匿名
function add-close-button(){
$('.msg').append('<a href="#" class="close">Close me</a>');
$('.msg .close').click(function(){ close-msg(); });
}
function close-msg(){ /* code here */ }
2. 定义函数+ HTML的onclick
function add-close-button(){
$('.msg').append('<a href="#" class="close" onclick="close-msg">Close me</a>');
}
function close-msg(){ /* code here */ }
3。 关闭功能没有定义,只是匿名的onclick功能
function add-close-button(){
$('.msg').append('<a href="#" class="close">Close me</a>');
$('.msg .close').click(function(){ /* code here */ });
}
使用代码格式化为固定代码 – hoijui
。我第一次做到了,但是我添加了一些评论让帖子更加清楚它的崩溃。谢谢 – roiwew