未接收参考:未在htmlbuttonelement.onclick wordpress Divi上定义FirstFunc
网络开发行业非常新,并且在弄清楚我的代码出了什么问题时遇到了一些麻烦。尝试使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是由一些错别字引起的。不幸的是,对于许多人来说,渴望在评论中列出它们。因此,如果这为您解决问题,请自愿删除您的问题:
如果(X.Style.backgroundColor ==“透明”;){
- &gt;删除分号:如果(X.Style.backgroundColor ==“透明”){
x.style.color =“ white”};
- &gt;交换分号和夹具:X.Style.Color =“ White”; }
(X.Style.backgroundColor =“透明”)
- &gt;卸下夹具并用半圆柱完成:X.Style.backgroundColor =“透明”;
getElementByClass
JusteGetElementsByClassName
。但是,这将返回一个数组。只需使用QuerySelector
。但是,更聪明的是使用CSS应用更改,并仅使用
.classlist.toggle('class-name');
函数: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:
if (x.style.backgroundColor == "transparent";) {
-> remove the semicolon:if (x.style.backgroundColor == "transparent") {
x.style.color = "white"};
-> swap the semicolon and clamp:x.style.color = "white"; }
(x.style.backgroundColor = "transparent")
-> remove the clamps and finish with a semicolon:x.style.backgroundColor = "transparent";
getElementByClass
justgetElementsByClassName
. However this would return an array. Just usequerySelector
instead.However, smarter would be to use CSS to apply the changes and just using the
.classList.toggle('class-name');
function: