js日常踩坑之onClick 获取 this 对象


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>';
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值