Javascript DOM遍历不';无法使用prev()?
我的妈妈看起来像这样 还有我的Jquery:Javascript DOM遍历不';无法使用prev()?,javascript,jquery,dom,Javascript,Jquery,Dom,我的妈妈看起来像这样 还有我的Jquery: $('.content').each(function(){ $(this).find('img').prev().removeAttr('height width style'); }); 为什么div还附带样式?您需要使用或喜欢 如果您有多个图像,那么您可以使用来自h3元素的图像 $('.content').each(function(){ $(this).find('h3').next('div').removeAttr(
$('.content').each(function(){
$(this).find('img').prev().removeAttr('height width style');
});
为什么div还附带样式?您需要使用或喜欢
如果您有多个图像,那么您可以使用来自h3元素的图像
$('.content').each(function(){
$(this).find('h3').next('div').removeAttr('height width style');
});
如果您只希望图像具有父div,那么您可以像这样尝试
$('.content > div').each(function(){
$(this).find('img').closest('div').removeAttr('height width style');
});
你可以用
$('.content').each(function(){
$(this).find('img').parent().closest('div').removeAttr('height width style');
});
希望这有帮助。如果您想使用较短的代码,则可以使用:
$('.content div[style]:has(img)').removeAttr('style');
这将起作用,因为您的div
是.content
div的子级,它除了样式
之外没有宽度、高度
属性
非工作演示:选择器上没有属性,因此不会受到影响
$('.content div[style]:has(img)').removeAttr('style');