现在代码是这样的。
<button id=hine onclick=mo('ppp')>Go!</button>
<script>
function mo(name){
alert(name);
alert($(this).attr('id')); // undefined
}
</script>如何在jQuery中使用$(this)?
发布于 2020-03-23 12:50:33
您需要将this作为显式参数传递。
<button id="hine" onclick="mo(this, 'ppp')">Go!</button>
<script>
function mo(element, name){
alert(name);
alert($(element).attr('id')); // undefined
}
</script>发布于 2020-03-23 12:50:25
尝尝这个
function mo(elem, name){
alert(name);
alert($(elem).attr('id'));
}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id=hine onclick="mo(this, 'ppp')">Go!</button>
https://stackoverflow.com/questions/60808036
复制相似问题