Javascript 如何在jQuery中使用多个选择器
以下是我使用的代码(工作正常,直到客户端上传新图像-然后必须对其进行修改才能再次工作:Javascript 如何在jQuery中使用多个选择器,javascript,jquery,html,Javascript,Jquery,Html,以下是我使用的代码(工作正常,直到客户端上传新图像-然后必须对其进行修改才能再次工作: $(文档).ready(函数(){ var firstProduct=$('.post-416.woocommerce-loop-product__title').html(); jQuery('.post-416h2.woocommerce-loop-product__title').remove(); var firstPrice=$('.post-416.price').html(); jQuery(
$(文档).ready(函数(){
var firstProduct=$('.post-416.woocommerce-loop-product__title').html();
jQuery('.post-416h2.woocommerce-loop-product__title').remove();
var firstPrice=$('.post-416.price').html();
jQuery('.post-416.price').remove();
console.log(firstProduct+firstPrice);
var secondProduct=$('.post-186.woocommerce-loop-product__title').html();
jQuery('.post-186 h2.woommerce-loop-product__title').remove();
var secondPrice=$('.post-186.price').html();
$('.post-186.price').remove();
var thirdProduct=$('.post-413.woocommerce-loop-product__title').html();
$('.post-413 h2.woocommerce-loop-product__title')。删除();
var thirdPrice=$('.post-413.price').html();
$('.post-413.price').remove();
var fourthProduct=$('.post-218.woocommerce-loop-product_utitle').html();
$('.post-218 h2.woommerce-loop-product__title').remove();
var fourthPrice=$('.post-218.price').html();
$('.post-218.price').remove();
变量linebreak=(“
”)
$(“.post-416.et_overlay”).append(firstProduct+linebreak+firstPrice);
$(.post-186.et_overlay”).append(第二个产品+换行符+第二个价格);
$(“.post-413.et_overlay”).append(第三个产品+换行符+第三个价格);
$(“.post-218.et_overlay”).append(第四个产品+换行符+第四个价格);
})
假设您的意思是不希望每次有人上传时都要添加一行,请使用通配符选择器:
jQuery('[class^="post-"] .woocommerce-loop-product__title').html()
它将选择所有元素.woocommerce-loop-product\uu title
,这些元素的父元素的类名以'post-'
开头。如果'post-'
不是这些元素中的第一个类名,请改用此名称:
jQuery('[class*="post-"] .woocommerce-loop-product__title').html()
用这样的东西
$(文档).ready(函数(){
/*如果post-*元素具有产品类,请使用它*/
var posts=$('.product'),//或$('[class*=“post-”])
换行符=“
”;
posts.each(函数(){
var self=$(此),
title=self.find('.woommerce-loop-product_uutitle'),
price=self.find(“.price”),
overlay=self.find('.et_overlay');
append(title.html()+linebreak+price.html());
title.remove();
price.remove();
});
});
这些是woocommerce产品吗?(post-*元素是否也有产品
类?).woocommerce-loop-product_uutitle').html();什么类型的元素是'post-*'
?可以使用该元素来代替?例如:jQuery('div.woocommerce-loop-product_utitle').html()
。还是您只想选择非常特定的4个元素?尝试选择特定的元素,每次图像上传到WordPress时都会发生变化(即:post-416可能会变成post-89)我需要选择post类,子类woocommerce-loop-product_uuu标题是这个,它肯定朝着正确的方向前进!谢谢。