简体   繁体   English

在wordpress中通过自定义JS单击后更改切换的背景颜色

[英]Changing background colour of toggle after click via custom JS in wordpress

I am currently trying to add a custom JS function (via the Live Code Editor Plugin) to my wordpress website. 我目前正在尝试将自定义JS函数(通过Live Code Editor插件)添加到我的wordpress网站。 The goal is to change the background colour of a toggle after click (ie from red -> green). 目的是在单击后更改切换器的背景颜色(即从红色->绿色)。 I have tried this function but although the selector works for CSS, the JS function is not working: 我已经尝试过此功能,但是尽管选择器适用于CSS,但JS功能不起作用:

CSS: CSS:

\\23 toggle-id-1 { background-color: red; }

JS: JS:

var \23 toggle-id-1 = document.getElementById("\23 toggle-id-1");

\\23 toggle-id-1.onclick = function(){ \\23 toggle-id-1.style.backgroundColor = "green"; }

In JSFiddle this worked without any problems, is there anything different for this plugin? 在JSFiddle中,此方法没有任何问题,此插件有什么不同之处吗?

Thank you! 谢谢!

jQuery solution: jQuery解决方案:

  $("#toggle-id-1").click(function () { $(this).toggleClass("green"); }); 
  #toggle-id-1 { background-color: red; height: 100px; width: 100px; } #toggle-id-1.green { background-color: green; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="toggle-id-1" class=""></div> 

You can't have spaces in variable names. 变量名称中不能包含空格。 Name your variable something else. 为变量命名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM