未接收参考:未在htmlbuttonelement.onclick wordpress Divi上定义FirstFunc

发布于 2025-01-29 15:54:53 字数 634 浏览 3 评论 0原文

网络开发行业非常新,并且在弄清楚我的代码出了什么问题时遇到了一些麻烦。尝试使用Divi将JavaScript实现在使用WordPress的代码段上,但似乎无法理解到底出了什么问题。我的意图是改变背景&单击时按钮的文字颜色。我感谢帮助!

<button onClick="firstFunc()";
 class="butCol";
 style="background-color: transparent;
 border: none;
 font-weight: bold;
 border-radius: 25px;
 padding: 2px 15px";>LATTES</button>

<script>
function firstFunc() {
 var x = document.getElementByClass("butCol");
  if (x.style.backgroundColor == "transparent";) {
    x.style.backgroundColor = "#373975"; 
    x.style.color = "white"};
  else {
    (x.style.backgroundColor = "transparent")
  };
};
</script>

Very new to the web development industry, and having some trouble figuring out what's wrong with my code. Trying to implement javascript onto a code snippet with Wordpress using Divi, but can't seem to understand what exactly is wrong. My intentions are to change the background & text color of the button on click. I'd appreciate the help!

<button onClick="firstFunc()";
 class="butCol";
 style="background-color: transparent;
 border: none;
 font-weight: bold;
 border-radius: 25px;
 padding: 2px 15px";>LATTES</button>

<script>
function firstFunc() {
 var x = document.getElementByClass("butCol");
  if (x.style.backgroundColor == "transparent";) {
    x.style.backgroundColor = "#373975"; 
    x.style.color = "white"};
  else {
    (x.style.backgroundColor = "transparent")
  };
};
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江城子 2025-02-05 15:54:53

问题是由一些错别字引起的。不幸的是,对于许多人来说,渴望在评论中列出它们。因此,如果这为您解决问题,请自愿删除您的问题:

  1. 如果(X.Style.backgroundColor ==“透明”;){ - &gt;删除分号:如果(X.Style.backgroundColor ==“透明”){
  2. x.style.color =“ white”}; - &gt;交换分号和夹具:X.Style.Color =“ White”; }
  3. (X.Style.backgroundColor =“透明”) - &gt;卸下夹具并用半圆柱完成:X.Style.backgroundColor =“透明”;
  4. 没有getElementByClass Juste GetElementsByClassName。但是,这将返回一个数组。只需使用QuerySelector
function firstFunc() {
 var x = document.querySelector(".butCol");
  if (x.style.backgroundColor == "transparent") {
    x.style.backgroundColor = "#373975"; 
    x.style.color = "white";
  } else {
    x.style.backgroundColor = "transparent";
  };
};
<button onClick="firstFunc()";
 class="butCol";
 style="background-color: transparent;
 border: none;
 font-weight: bold;
 border-radius: 25px;
 padding: 2px 15px";>LATTES</button>

但是,更聪明的是使用CSS应用更改,并仅使用.classlist.toggle('class-name');函数:

function firstFunc() {
 document.querySelector('.butCol').classList.toggle('clicked'); 
}
.butCol {
  background-color: transparent;
  border: none;
  border-radius: 25px;
  padding: 2px 15px;
  font-weight: bold;
}
  
.clicked {
  background-color: #373975;
  color: white;
}
<button onClick="firstFunc()";
 class="butCol";>LATTES</button>

The issue is caused by a few typos. unfortunately to many and to long to list them in the comments. So if this solve the issue for you, remove your question voluntarily:

  1. if (x.style.backgroundColor == "transparent";) { -> remove the semicolon: if (x.style.backgroundColor == "transparent") {
  2. x.style.color = "white"}; -> swap the semicolon and clamp: x.style.color = "white"; }
  3. (x.style.backgroundColor = "transparent") -> remove the clamps and finish with a semicolon: x.style.backgroundColor = "transparent";
  4. There is no getElementByClass just getElementsByClassName. However this would return an array. Just use querySelector instead.

function firstFunc() {
 var x = document.querySelector(".butCol");
  if (x.style.backgroundColor == "transparent") {
    x.style.backgroundColor = "#373975"; 
    x.style.color = "white";
  } else {
    x.style.backgroundColor = "transparent";
  };
};
<button onClick="firstFunc()";
 class="butCol";
 style="background-color: transparent;
 border: none;
 font-weight: bold;
 border-radius: 25px;
 padding: 2px 15px";>LATTES</button>

However, smarter would be to use CSS to apply the changes and just using the .classList.toggle('class-name'); function:

function firstFunc() {
 document.querySelector('.butCol').classList.toggle('clicked'); 
}
.butCol {
  background-color: transparent;
  border: none;
  border-radius: 25px;
  padding: 2px 15px;
  font-weight: bold;
}
  
.clicked {
  background-color: #373975;
  color: white;
}
<button onClick="firstFunc()";
 class="butCol";>LATTES</button>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文