首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用javascript更改woocommerce类

如何使用javascript更改woocommerce类
EN

Stack Overflow用户
提问于 2020-11-03 11:21:37
回答 1查看 35关注 0票数 2

我有一个包含"woocommerce columns-3“类的类别,我想将其更改为"woocommerce columns-4”。我如何使用javascript来做这件事?

我已经尝试过这个代码,但没有成功:

代码语言:javascript
复制
<script type="text/javascript"> 
document.getElementById("main").classList.add('woocommerce columns-4');
document.getElementById("main").classList.remove('woocommerce columns-3');
</script>

HTML

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-03 12:16:21

您必须以正确的元素为目标才能使其工作。在您的代码中,您正尝试以id为main的元素为目标。

我建议您使用document.querySelector(),这样您的代码将为:

代码语言:javascript
复制
<script type="text/javascript">
// select element        
var ourElement=document.querySelector(".elementor-widget-container .woocommerce.columns-3");
ourElement.classList.remove('columns-3');
ourElement.classList.add('columns-4');
</script>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64656373

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档