"this"一词的含义是什么?在jQuery脚本中 [英] what is the meaning of the word "this" in Jquery script

查看:98
本文介绍了"this"一词的含义是什么?在jQuery脚本中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是JavaScript和JQuery语言的新手.我开始看到一些JQuery脚本示例.

Hello I'm a newcomer in JavaScript and JQuery language. I started to see some examples of JQuery script.

我有以下代码段:

 <script type="text/javascript">

   $(document).ready(function(){

   $("p").click(function(){

   $(this).hide();

    });

  });

</script>

我的问题:在此代码行中的"this" 一词是什么意思?

My question: what is the meaning of word "this" in this line of code:

          $(this).hide();

推荐答案

它引用选择器$("p") 中被点击的每个p标记.例如,您可以看到这样单击的每个p标签的html:

It refers to each of p tags in your selector $("p") that gets clicked. For example, you can see html of each p tag you clicked on like this:

$("p").click(function(){
   alert($(this).html());
});

还请注意,上述上下文中的$(this)this表示不同的含义.后面的this指的是DOM元素本身,它没有可用的jQuery方法/属性,例如:

Notice also that $(this) and this in above context mean different things. The latter this refers to DOM element itself which won't have jQuery methods/properties available to it, for example:

$("p").click(function(){
   alert(this.html());
});

不起作用,因为html()将不可用,因为this引用其中的DOM元素.因此,如果要使用jQuery方法,请改用$(this).

Won't work because html() won't be available because this refers to DOM element in there. So if you want to use jQuery methods, use $(this) instead.

这篇关于"this"一词的含义是什么?在jQuery脚本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆