简体   繁体   English

来回改变风格

[英]Change style back and forth

Code: 码:

<style> .demo {color: yellow; background-color: purple} </style>
<p class="demo">my text</p>
<button id="change">click</button>
<script>
window.onload = function () {
var flipColors = query("#change"); 
  var target = query(".demo");
  flipColors.onclick = function() {
    style(target, "color", "white");
    style(target, "background-color", "black");
  };
};
</script>

When I click the button the style changes. 当我单击按钮时,样式会发生变化。

My question is what do I need to add to the JS that when the button clickes again - it would restore the original style, and if it is clicked again it will activate the function again, instead of making two buttons. 我的问题是我需要向JS添加什么,当按钮再次点击时 - 它将恢复原始样式,如果再次单击它将再次激活该功能,而不是制作两个按钮。

I know that I can create .demo and .demo1 classes and switch them, but I want to understand how to make it that way - if it is possible. 我知道我可以创建.demo.demo1类并切换它们,但我想了解如何以这种方式实现 - 如果有可能的话。

Note: Just plain JS, not JQuery etc. 注意:只是简单的JS,而不是JQuery等。

yup, the toggle() works wonderfully well here. 是的,toggle()在这里运行得非常好。 I did create a two toggle classes, and the javascript simply turns one on initially, then toggles the two states. 我确实创建了两个切换类,javascript最初只打开一个,然后切换两个状态。

 var flipColors = query("#change"); var target = query(".demo"); target.classList.add("demo-on"); toggleClasses = function toggleClasses() { target.classList.toggle("demo-on"); target.classList.toggle('demo-off'); }; 
 .demo-off { color: yellow; background-color: purple; } .demo-on { color: purple; background-color: yellow; } 
 <p class="demo">my text</p> <button id="change" onclick="toggleClasses()">click</button> 

Ah, but you don't want to actually toggle the classes, but the css attributes? 啊,但你不想实际切换类,但css属性? Easily done. 轻松完成。 Try this one: 试试这个:

 var theToggle = document.getElementById("change"); var toggleMe = document.getElementsByClassName("demo")[0]; toggleMe.toggleStatus = "on"; theToggle.onclick = function(){ switch(toggleMe.toggleStatus){ case "on": toggleMe.toggleStatus="off"; toggleMe.style.color = "purple"; toggleMe.style.backgroundColor = "yellow"; break; case "off": toggleMe.toggleStatus="on"; toggleMe.style.color = "yellow"; toggleMe.style.backgroundColor = "purple"; break; } } 
 .demo { width: 100px; height: 100px; color: yellow; background-color: purple } 
 <p class="demo">my text</p> <button id="change">click</button> 

This is pure js, I'm adding a property that I use to check which status to use but you could just as easily run the switch statement off, for example, toggleMe.style.color -- Hope this helps! 这是纯粹的js,我添加了一个属性,我用它来检查要使用的状态,但你可以轻松地关闭switch语句,例如,toggleMe.style.color - 希望这会有所帮助!

The word I think you're looking for is "Toggle". 我认为你正在寻找的这个词是“Toggle”。 You want something to go from Active to Inactive and back and forth with each user interaction, ie a click event. 您希望某些内容从“活动”变为“非活动”,并在每次用户交互时来回切换,即点击事件。

Javascript has a function that handles that with a class. Javascript有一个用类处理它的函数。 For example, the following line of code: 例如,以下代码行:

target.classList.toggle("active");

would add "active" as a class to the object target if it's absent, and remove it if it's present. 将“活动”作为类添加到对象target如果它不存在),并将其删除(如果它存在)。 Here is more info on HTML DOM classList Property with toggle and other methods. 以下是有关toggle和其他方法的HTML DOM classList属性的更多信息。

But if you were looking for a jQuery solution, there's a similar function: 但是如果你正在寻找一个jQuery解决方案,那么它有一个类似的功能:

$(".class").toggleClass("active"); 

And here's the documentation for the .toggleClass() jQuery method. 这是.toggleClass() jQuery方法的文档。

If you are trying to achieve toggle behaviour using javascript 如果您尝试使用javascript实现切换行为

you can do as the following code snippet 您可以执行以下代码段

check this snippet 检查这个片段

 window.onload = function() { var flipColors = document.querySelector("#change"); var target = document.querySelector(".demo"); flipColors.onclick = function() { toggleStylechanges(target, "color", "white"); toggleStylechanges(target, "backgroundColor", "black"); }; } function toggleStylechanges(element, cssProp, color) { if (element.style[cssProp] == "") element.style[cssProp] = color; else element.style[cssProp] = ""; } 
 .demo { color: yellow; background-color: purple } 
 <p class="demo">my text</p> <button id="change">click</button> 

Hope it helps 希望能帮助到你

Try something like this: 尝试这样的事情:

window.onload = function () {
  var flipColors = query("#change"); 
  var target = query(".demo");
  var clicked = false;
  flipColors.onclick = function() {
    if(clicked) {
      style(target, "color", "yellow");
      style(target, "background-color", "purple");
    } else {
      style(target, "color", "white");
      style(target, "background-color", "black");
    }
    clicked = !clicked;
  };
};

Shorter version: 更短的版本:

window.onload = function () {
  var flipColors = query("#change"); 
  var target = query(".demo");
  var clicked = false;
  flipColors.onclick = function() {
    var color = clicked ? "yellow" : "white";
    var bgColor = clicked ? "purple" : "black";
    style(target, "color", color);
    style(target, "background-color", bgColor);
    clicked = !clicked;        
  };
};

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

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