Javascript $(这)在jquery中是什么意思 演示 风险值标题=$('h3'); 风险价值=美元('p'); 等式(0).show(); 标题。单击(函数(){ var cur=$(this);//保存已单击的元素以便于引用 cur.sides('p').hide();//隐藏所有段落 cur.next('p').show();//获取单击标题后的下一段并显示它 }); p、 h3{边距:0;填充:0;} p{高度:150px;宽度:200px;边框:1px纯黑;} h3{高度:50px;宽度:200px;背景色:蓝色;颜色:白色;边框:1px纯黑色;} 项目1
项目1内容 项目2 项目2内容 项目3 项目3内容 项目4 项目4内容 以上代码取自此处: 问题: 对于此行:Javascript $(这)在jquery中是什么意思 演示 风险值标题=$('h3'); 风险价值=美元('p'); 等式(0).show(); 标题。单击(函数(){ var cur=$(this);//保存已单击的元素以便于引用 cur.sides('p').hide();//隐藏所有段落 cur.next('p').show();//获取单击标题后的下一段并显示它 }); p、 h3{边距:0;填充:0;} p{高度:150px;宽度:200px;边框:1px纯黑;} h3{高度:50px;宽度:200px;背景色:蓝色;颜色:白色;边框:1px纯黑色;} 项目1,javascript,jquery,Javascript,Jquery,项目1内容 项目2 项目2内容 项目3 项目3内容 项目4 项目4内容 以上代码取自此处: 问题: 对于此行:var cur=$(此)我知道这个是指被点击的h3,但是为什么我们不能这样写:var cur=this这里的这个和$(这个)有什么区别 <!doctype html> <html> <head> <meta charset="utf-8" /> <title>Demo</title
var cur=$(此)
我知道这个
是指被点击的h3
,但是为什么我们不能这样写:var cur=this
这里的这个
和$(这个)
有什么区别
<!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>
将此返回的DOM元素转换为jQuery对象,从中可以继续在上使用jQuery。
此
是对调用当前函数的成员的引用
比如说
$(this)
现在你可以用它做更多的事情了
此
返回DOM元素,而$(此)
返回jQuery对象
$(document).ready(function(){
$("#test").click(function(){
var jQuerizedElement = $( this ); // instead of calling it by its selector you can use this
});
});
相当于
this
$
是jQuery函数的缩写。你也可以写:
$(this).get()
jQuery“包装”DOM元素和其他对象,以便为它们提供更多功能。就像将选择器字符串传递到jQuery构造函数中一样,您可以传递一个本机DOM元素(这是this
),它将成为jQuery对象
$(document).ready(function(){
$("#test").click(function(){
var jQuerizedElement = $( this ); // instead of calling it by its selector you can use this
});
});