扫码关注官方订阅号
认证高级PHP讲师
例子代码: http://codepen.io/hj624608494...
问题出现在 this的指向 this永远指向函数执行时的对象.
function choose(){ alert($(this).data('name')); // undefind 因为choose函数的this指向的是window } function choose2(){ $('#J_a').click(function(){ // 这个点击事件的匿名函数的this 指向的是 $('#J_a') 这个对象 alert($(this).data('name')); }) } choose2()
绑定事件的时候把this传进去,onclick="choose(this)"
function choose(x){
alert(x.getAttribute("data-name"))
}
$(this).attr('data-name');
获取属性 - attr()jQuery attr() 方法用于获取属性值。
$('#toy').attr('data-name');
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
例子代码: http://codepen.io/hj624608494...
问题出现在 this的指向 this永远指向函数执行时的对象.
绑定事件的时候把this传进去,onclick="choose(this)"
function choose(x){
}
$(this).attr('data-name');
获取属性 - attr()
jQuery attr() 方法用于获取属性值。
$('#toy').attr('data-name');