$(this)在jquery中是什么意思 [英] what does $(this) mean in jquery
本文介绍了$(this)在jquery中是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Demo</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
var headings = $('h3');
var paras = $('p');
paras.hide().eq(0).show();
headings.click(function() {
var cur = $(this); //save the element that has been clicked for easy referal
cur.siblings('p').hide(); //hide all the paragraphs
cur.next('p').show(); //get the next paragraph after the clicked header and show it
});
</script>
<style type="text/css">
p,h3 {margin: 0; padding: 0;}
p {height: 150px; width: 200px; border: 1px solid black;}
h3 {height: 50px; width: 200px; background-color: blue; color: white; border: 1px solid black;}
</style>
</head>
<body>
<h3>Item 1 </h3>
<p>Item 1 content</p>
<h3>Item 2 </h3>
<p>Item 2 content</p>
<h3>Item 3 </h3>
<p>Item 3 content</p>
<h3>Item 4</h3>
<p>Item 4 content</p>
</body>
</html>
以上代码来自此处: http://www.1stwebdesigner.com/tutorials/jquery-for-complete-beginners-part-3/
问题:
:var cur = $(this);
我知道this
表示已单击的h3
,但是为什么我们不能这样写:var cur = this;
这里的this
和$(this)
有什么区别?
for this line: var cur = $(this);
I know this
means the h3
that has been clicked, but why we can not write this way: var cur = this;
what is the difference between this
and $(this)
here?
推荐答案
$(this)
将this
返回的DOM元素转换为jQuery对象,从中可以继续使用jQuery.
converts the DOM element returned by this
to a jQuery object, from which, you can continue using jQuery on.
这篇关于$(this)在jquery中是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文