jQ version: jquery-1.7.2.js
<div id="box">test</div>
<script> $(function(){ $('#box').click(function(){ //alert(this.html()); // 报错 alert(this.innerHTML);// test alert($(this).html()); // test }); });</script>
Please tell me, after getting through $('#box'), the jQuery object has been obtained , why does this correspond to DOM object, but $(this) is a jQuery object? Thanks!
You have to know that there is js without jquery, and there is no jquery without js. This is the stuff of js. FunctionThis in the execution environment belongs to native js. How can it point you to the jquery object. Wearing $(this) vest is the jquery object.
$('#box') has returned the JQuery object, so you can only call the click() method later. Please note that this step is quite complicated. Invar $box = $('#box'); $box.click(function(){ //TODO});
event to the DOM. When the event is triggered, this It’s just that this DOM
Look at the following code to get the object that triggered the event through the event object:
$('.box').click(function(event){ console.log(event.target); console.log(event.target==this);//非事件冒泡情况下为true console.log($(this).get(0)==this);//true});
The above is the detailed content of jQuery: Problem solving using this in click() method. For more information, please follow other related articles on the PHP Chinese website!