扫码关注官方订阅号
Document a a a a
a
光阴似箭催人老,日月如移越少年。
each当中的this并不是jQuery对象,而是原生的DOM,所以没法调用css方法
var texts = $('.text') texts.each(function(index,element) { $(this).css('color','red') });
jQuery存在隐式迭代的功能,这样写即可改写全部的样式
var texts = $('.text') texts.css('color','red');
jquery 的所有回调函数内 this 是dom对象,css是jquery对象才有方法解决办法:
$(this).css...
$(this).css(...)
在 each() 中的 this 指向的是 普通 dom 對象 ,並不是經過 jquery 包裝過的對象,所以你得再次包裝:
each()
this
普通 dom 對象
jquery
$(this).css('color','red')
this是dom对象,应该用$(this).css
this指向的是当前DOM元素,css方法是jquery对象才会有的,因此需要将this转化为Jquery对象$(this)
css
Jquery
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
each当中的this并不是jQuery对象,而是原生的DOM,所以没法调用css方法
jQuery存在隐式迭代的功能,这样写即可改写全部的样式
jquery 的所有回调函数内 this 是dom对象,css是jquery对象才有方法
解决办法:
在
each()中的this指向的是普通 dom 對象,並不是經過jquery包裝過的對象,所以你得再次包裝:this是dom对象,应该用$(this).css
this指向的是当前DOM元素,css方法是jquery对象才会有的,因此需要将this转化为Jquery对象$(this)