转自:http://www.oschina.net/code/snippet_111193_15135
1. [代码][JavaScript]代码
var js = "alert('B:' + this.id); return false;";
// creates a function from the "js" string
var newclick = eval("(function(){"+js+"});");
// clears onclick then sets click
$("#anchor").attr('onclick', '').click(newclick);
2. [代码]或者
//如果onclick事件原先有值,要先清空,再用click( eval(function(){.....}) )赋值:
$("input[name='orderCar']").attr('onclick','').click( eval(function(){warningT()}));
function warningT(){
alert("看到了吧!");
return;
}
3. 又或者 (不用eval 感觉更直接)
//显示消息窗口
showWarning: function (msg, icon, callback) {
myUI.showMask("black", 0.6); //遮罩(底色:黑,透明度:0.6)
var html = "<div id='show-warning' class='mod-form__overlay ui-shadow' style='width: 240px; height: 100px; margin-left: -130px;'>" +
"<div id='btn-closewarning' class='mod-form__overlay-close'>X</div>" +
"<div class='ui-mt-medium'>" +
"<span id='msg_icon' class='icon'></span><p id='msg_content' class='ui-fz-medium ui-c-primary' sytle='line-height: 19.6px; margin-top: 10px;'></p><p></p>" +
"</div></div>"
if ($("#show-warning").length == 0) {
$("#form-mask").after(html);
}
//绑事件(先清,再绑)
$("#btn-closewarning").attr("onclick", "").click(function () {
//隐藏提示窗
myUI.hideMask();
$("#show-warning").css("display", "none");
//回调
if (callback) {
callback();
}
});
//显示
$("#show-warning").css("display", "block");
//图标
$("#msg_icon").addClass("icon_" + icon);
$("#msg_content").text(msg);
},