$(这)在jQuery中是什么意思
what does $(this) mean in 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)
有什么区别?
$(this)
将 this
返回的 DOM 元素转换为 jQuery 对象,从中可以继续使用 jQuery on。
this
是对调用当前函数的成员的引用
例如
$(document).ready(function(){
$("#test").click(function(){
var jQuerizedElement = $( this ); // instead of calling it by its selector you can use this
});
});
现在你可以用它做更多的事情。
>this
返回 DOM 元素,而$(this)
返回 jQuery 对象。
this
相当于
$(this).get()
$
是jQuery函数的简写。你也可以写:
var cur = jQuery(this);
jQuery"包装"了DOM元素和其他对象,以便为它们提供更多功能。就像将选择器字符串传递到 jQuery 构造函数中一样,您可以传递一个本机 DOM 元素(this
(,它将成为 jQuery 对象。
相关文章:
- @@(“at at”)在ES6 JavaScript中是什么意思
- minimy javascript是什么意思?Backbone.js
- var w = q||问2;这个表达式是什么意思
- 函数(i,val)在javascript中是什么意思
- 问号在这段代码中是什么意思
- 我不知道此代码中的这些符号是什么意思.十进制到二进制
- ||{}在javascript中是什么意思
- “转义字符串”在Regex中是什么意思?(Javascript)
- 这个代码是什么意思(history.replaceState和history.pushstate)
- :: 是什么意思
- 这是什么意思?getData([renderData]);
- MDN文档中关于弄清楚XUL元素的段落是什么意思:“如何将覆盖扩展转换为无重启”
- require('file_path')后面的后缀(模块)在javascript文件中是什么意思
- Twitter Bootstrap的Tab插件中“data-api”是什么意思
- 箭头函数 '() => {}' 在 Javascript 中是什么意思?
- 在这种情况下,“foo[x]”是什么意思?
- 在 http 请求中添加事件侦听器 ( request.on ) 是什么意思?而笏就是它的用途
- 这种格式是什么意思 T00:00:00.000Z
- 这行是什么意思“ operator.indexOf(btnVal) > -1”
- JQuery:这段代码是什么意思