js日常踩坑,onClick 获取 this 对象
使用的是 bootstraptable 控件,在返回数据时 formatter 要设置值在 a 标签中,一直 1 方法中这样传入 this,一直错误: Uncaught SyntaxError: Unexpected identifier ,这是因为此 this 被当成定义的入参了,非函数调用时的上下文,所以后面 skip 函数中获取 this 一直为 Underfined
function skipTransactionCode(value, data) {
if (value == 1) {
// 1.重点,这是错误的,这样的 this 就类似于传入的参,所以报错 - Uncaught SyntaxError: Unexpected identifier
return '<a οnclick="skip(' + this + ',\'' + data.transaction_date + '\',' + data.transaction_code + ');">' + "进账" + '</a>';
} else {
// 2.函数调用时,this 指向函数上下文
return '<a οnclick="skip(this,\'' + data.transaction_date + '\',' + data.transaction_code + ');">' + "出账" + '</a>';
}
}