jQuery this选择器,选取当前元素

this选择器

jQuery的this选择器,一般用于选取已经被选取到的HTML元素,比如在定义jQuery的按钮的点击处理函数中,就可以使用this选择器选取按钮本身,具体的可参考下方的实例代码。this选择器的语法如下:

$(this)

重点提示:注意,语法中的this并没有使用引号包裹!

jQuery this选择器实例代码

下方的实例中,通过this选择器选取了按钮本身:

<p>jQuery的this选择器一般与其它的选择器搭配使用。比如下方script标签内的jQuery代码。通过this选取button,并修改其大小。</p>

<button id="example">点击</button>
<script>
   $("#example").click(function(){
     $(this).css({"width":"50px","height":"50px"});
   })
</script>

免责声明:内容仅供参考,不保证正确性!


全栈前端 / jQuery教程 :






Copyright © 2022-2024 笨鸟工具 x1y1z1.com All Rights Reserved.