JavaScript函数切换CSS类未按预期工作。

huangapple go评论80阅读模式
英文:

Javascript function to switch the css classes not working as intended

问题

我有一个HTML文件,它使用td元素创建了一个5*4的网格。每个td元素都有它的CSS类,具有一些属性。

我尝试在按钮点击时切换

元素的CSS类。例如,如果我点击"up"按钮,每一行的CSS类应该与下一行的CSS类交换,而第一行应该移到最后一行。

function switchColorUp() {

  var allTDs = document.getElementsByTagName("td");

  for (var i = 0; i < allTDs.length; i++) {

    allTDs[i].classList = allTDs[(i + 4) % 20].classList;

  }

}

Codepen链接:

https://codepen.io/001Abhishek/pen/NWzrZyg

它可以工作,但第二行的CSS会同时覆盖第一行和最后一行。因此,第一行的CSS会丢失(请查看Codepen链接以验证这种行为)。

似乎无法弄清楚如何处理这种情况。任何帮助将不胜感激。

我尝试将第一行元素保存在临时变量中,然后将它们与最后一行交换,并在倒数第二行之前运行循环。

英文:

I have an HTML file which creates a 5*4 grid using td elements. Each td element has it's css class with bunch of properties.

I'm trying to switch the css classes of my <td> elements on a button click. Example, if i click 'up' button the css classes of each row should be swapped with css class of the row below, and first row should go to the last one.

function switchColorUp() {

  var allTDs = document.getElementsByTagName(&quot;td&quot;);

  

 for (var i = 0; i &lt; allTDs.length; i++) {

  allTDs[i].classList = allTDs[(i + 4) % 20].classList;

   }

}


Codepen link-

https://codepen.io/001Abhishek/pen/NWzrZyg

It works except for the second row's css is overlapping the first and last row at same time. Hence the first row's css gets lost( check the codepen link to verify this behaviour)

Can't seem to figure out how to handle that case. Any help will be appreciated.

I tried saving first row elements in temp var and then swapping them with the last row and running the for loop till the second last row.

答案1

得分: 1

第一个行的CSS丢失的原因是由于for循环覆盖了前4个值。for循环的最后4次迭代(即i = 16,17,18,19)会获取覆盖的前4个元素的CSS。

将第一行元素保存到临时变量中也不起作用,因为classList返回的是一个Live DOMTokenList,可以从文档中看到,它本质上是一个引用。

我的解决方案涉及使用toString()函数将前4个元素保存为值:

function switchColorUp() {
  var allTDs = document.getElementsByTagName("td");
  var temp1 = allTDs[0].classList.toString();
  var temp2 = allTDs[1].classList.toString();
  var temp3 = allTDs[2].classList.toString();
  var temp4 = allTDs[3].classList.toString();
  
  for (var i = 0; i < allTDs.length; i++) {
    allTDs[i].classList = allTDs[(i + 4) % 20].classList;
  }
  allTDs[16].classList = temp1;
  allTDs[17].classList = temp2;
  allTDs[18].classList = temp3;
  allTDs[19].classList = temp4;
}

希望这有所帮助!

英文:

The reason why the first row's css gets lost is due to the for loop overwriting the first 4 values. The last 4 iterations of the for loop (ie. i = 16,17,18,19) would get the css for the overwritten first 4 elements.

The reason why saving the first row elements into a temp var does not work either is because classList returns a live DOMTokenList as seen from the documentation, which is essentially a reference.

My solution involves saving the first 4 elements as values using the toString() function:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function switchColorUp() {
  var allTDs = document.getElementsByTagName(&quot;td&quot;);
  var temp1 = allTDs[0].classList.toString();
  var temp2 = allTDs[1].classList.toString();
  var temp3 = allTDs[2].classList.toString();
  var temp4 = allTDs[3].classList.toString();
  
 for (var i = 0; i &lt; allTDs.length; i++) {
  allTDs[i].classList = allTDs[(i + 4) % 20].classList;
 }
  allTDs[16].classList = temp1
  allTDs[17].classList = temp2;
  allTDs[18].classList = temp3;
  allTDs[19].classList = temp4;
}

<!-- language: lang-css -->

body {
  background-color: black;
}
.table {
  font-size: 1.5em;
  color: white;
  font-style: times new roman;
}
.td1 {
  border: 6px solid red;
}
.td1:hover {
  background-color: red;
  color: black;
  transform: scale(1.2);
  opacity: 1;
  transition: 0.5s;
}
.td2 {
  border: 6px solid green;
}
.td2:hover {
  background-color: green;
  color: black;
  transform: scale(1.2);
  opacity: 1;
  transition: 0.5s;
}
.td3 {
  border: 6px solid orangered;
}
.td3:hover {
  background-color: orangered;
  color: black;
  transform: scale(1.2);
  opacity: 1;
  transition: 0.5s;
}
.td4 {
  border: 6px solid blue;
}
.td4:hover {
  background-color: blue;
  color: black;
  transform: scale(1.2);
  opacity: 1;
  transition: 0.5s;
}
.td5 {
  border: 6px solid purple;
}
.td5:hover {
  background-color: purple;
  color: black;
  transform: scale(1.2);
  opacity: 1;
  transition: 0.5s;
}
.td6 {
  border: 6px solid brown;
}
.td6:hover {
  background-color: brown;
  color: black;
  transform: scale(1.2);
  opacity: 1;
  transition: 0.5s;
}
.td7 {
  border: 6px solid YELLOW;
}
.td7:hover {
  background-color: yellow;
  color: black;
  transform: scale(1.2);
  opacity: 1;
  transition: 0.5s;
}
.td8 {
  border: 6px solid silver;
}
.td8:hover {
  background-color: silver;
  color: black;
  transform: scale(1.2);
  opacity: 1;
  transition: 0.5s;
}
.td9 {
  border: 6px solid pink;
}
.td9:hover {
  background-color: pink;
  color: black;
  transform: scale(1.2);
  opacity: 1;
  transition: 0.5s;
}

.td10 {
  border: 6px solid cyan;
}
.td10:hover {
  background-color: cyan;
  opacity: 1;
  color: black;
  transform: scale(1.2);
  transition: 0.5s;
}
.td11 {
  border: 6px solid lightgreen;
}
.td11:hover {
  background-color: lightgreen;
  opacity: 1;
  color: black;
  transform: scale(1.2);
  transition: 0.5s;
}
.td12 {
  border: 6px solid teal;
}
.td12:hover {
  background-color: teal;
  opacity: 1;
  color: black;
  transform: scale(1.2);
  transition: 0.5s;
}

.td13 {
  border: 6px solid navy;
}
.td13:hover {
  background-color: navy;
  opacity: 1;
  color: black;
  transform: scale(1.2);
  transition: 0.5s;
}

.td14 {
  border: 6px solid salmon;
}
.td14:hover {
  background-color: salmon;
  opacity: 1;
  color: black;
  transform: scale(1.2);
  transition: 0.5s;
}

.td15 {
  border: 6px solid olive;
}
.td15:hover {
  background-color: olive;
  opacity: 1;
  color: black;
  transform: scale(1.2);
  transition: 0.5s;
}

.td16 {
  border: 6px solid darkslategray;
}
.td16:hover {
  background-color: darkslategray;
  opacity: 1;
  color: black;
  transform: scale(1.2);
  transition: 0.5s;
}

.td17 {
  border: 6px solid magenta;
}
.td17:hover {
  background-color: magenta;
  opacity: 1;
  color: black;
  transform: scale(1.2);
  transition: 0.5s;
}

.td18 {
  border: 6px solid white;
}
.td18:hover {
  background-color: white;
  opacity: 1;
  color: black;
  transform: scale(1.2);
  transition: 0.5s;
}

.td19 {
  border: 6px solid khaki;
}
.td19:hover {
  background-color: khaki;
  opacity: 1;
  color: black;
  transform: scale(1.2);
  transition: 0.5s;
}

.td20 {
  border: 6px solid yellowgreen;
}
.td20:hover {
  background-color: yellowgreen;
  opacity: 1;
  color: black;
  transform: scale(1.2);
  transition: 0.5s;
}
.btn1 {
  height: 550px;
  width: 400px;
  position: relative;
  margin: -570px 500px;
  background-color: rgba(0, 0, 0, 0.7);
  box-shadow: 1px 1px 10px 5px deepskyblue;
}

.button {
  cursor: pointer;
  position: absolute;
  left: 105px;
  top: 100px;
  color: deepskyblue;
  background-color: transparent;
  border-radius: 10%;
  border: 1px solid deepskyblue;
}

.button1 {
  background-color: transparent;
  cursor: pointer;
  color: deepskyblue;
  border: 1px solid deepskyblue;
  position: absolute;
  left: 40px;
  top: 150px;
  border-radius: 10%;
}

.button2 {
  cursor: pointer;
  color: deepskyblue;
  background-color: transparent;
  border: 1px solid deepskyblue;
  border-radius: 10%;

  position: absolute;
  left: 160px;
  top: 150px;
}

.button3 {
  color: deepskyblue;
  background-color: transparent;
  cursor: pointer;
  border: 1px solid deepskyblue;
  position: absolute;
  left: 100px;
  top: 200px;
  border-radius: 10%;
}
.name11 {
  color: deepskyblue;
  position: absolute;
  left: 5px;
  top: 250px;
}
.butt {
  cursor: pointer;
  position: absolute;
  left: 60px;
  top: 310px;
  color: deepskyblue;
  background-color: deepskyblue;
  border-radius: 50%;
  border: 1px solid deepskyblue;
}

.butt1 {
  cursor: pointer;
  position: absolute;
  left: 180px;
  top: 310px;
  color: deepskyblue;
  background-color: deepskyblue;
  border-radius: 50%;
  border: 1px solid deepskyblue;
}

.butt2 {
  cursor: pointer;
  position: absolute;
  left: 180px;
  top: 380px;
  color: deepskyblue;
  background-color: deepskyblue;
  border-radius: 50%;
  border: 1px solid deepskyblue;
}

.butt3 {
  cursor: pointer;
  position: absolute;
  left: 60px;
  top: 380px;
  color: deepskyblue;
  background-color: deepskyblue;
  border-radius: 50%;
  border: 1px solid deepskyblue;
}

.name {
  color: deepskyblue;
  position: absolute;
  left: 5px;
  top: 5px;
}

.pass {
  color: deepskyblue;
  font-size: 1em;
  font-family: book antiqua;
  position: absolute;
  left: 40px;
  top: 450px;
}
.pass:click {
  border: 1px solid deepskyblue;
}

<!-- language: lang-html -->

&lt;html&gt;

&lt;head&gt;
  &lt;title&gt;login system&lt;/title&gt;
  &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;login.css&quot;&gt;
&lt;/head&gt;

&lt;body&gt;

  &lt;table class=&quot;table&quot; cellpadding=6px cellspacing=10px&gt;

    &lt;tr&gt;
      &lt;td class=&quot;td1&quot;&gt;
        A &amp;nbsp&amp;nbsp B&lt;br&gt;&lt;br&gt;C &amp;nbsp&amp;nbsp D
      &lt;/td&gt;

      &lt;td class=&quot;td2&quot;&gt;
        U &amp;nbsp&amp;nbsp V&lt;br&gt;&lt;br&gt;W &amp;nbsp&amp;nbsp X
      &lt;/td&gt;

      &lt;td class=&quot;td3&quot;&gt;
        = &amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp (&lt;br&gt;&lt;br&gt;+ &amp;nbsp&amp;nbsp&amp;nbsp _
      &lt;/td&gt;

      &lt;td class=&quot;td4&quot;&gt;
        a &amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp b&lt;br&gt;&lt;br&gt;c &amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp d
      &lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td class=&quot;td5&quot;&gt;
        E &amp;nbsp&amp;nbsp F&lt;br&gt;&lt;br&gt;G &amp;nbsp&amp;nbsp H
      &lt;/td&gt;

      &lt;td class=&quot;td6&quot;&gt;
        Y &amp;nbsp&amp;nbsp&amp;nbsp Z&lt;br&gt;&lt;br&gt;1 &amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp 2
      &lt;/td&gt;

      &lt;td class=&quot;td7&quot;&gt;
        { &amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp ]&lt;br&gt;&lt;br&gt;$ &amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp !
      &lt;/td&gt;

      &lt;td class=&quot;td8&quot;&gt;
        e &amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp f&lt;br&gt;&lt;br&gt;g &amp;nbsp&amp;nbsp&amp;nbsp h
      &lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td class=&quot;td9&quot;&gt;
        I &amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp J&lt;br&gt;&lt;br&gt;K &amp;nbsp&amp;nbsp&amp;nbsp L
      &lt;/td&gt;

      &lt;td class=&quot;td10&quot;&gt;
        3 &amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp 4&lt;br&gt;&lt;br&gt;5 &amp;nbsp &amp;nbsp&amp;nbsp 6
      &lt;/td&gt;

      &lt;td class=&quot;td11&quot;&gt;
        i &amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp j&lt;br&gt;&lt;br&gt;k &amp;nbsp &amp;nbsp&amp;nbsp l
      &lt;/td&gt;

      &lt;td class=&quot;td12&quot;&gt;
        m &amp;nbsp&amp;nbsp n&lt;br&gt;&lt;br&gt;o &amp;nbsp &amp;nbsp&amp;nbsp p
      &lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td class=&quot;td13&quot;&gt;
        M &amp;nbsp&amp;nbsp N&lt;br&gt;&lt;br&gt;O &amp;nbsp &amp;nbsp&amp;nbsp P
      &lt;/td&gt;

      &lt;td class=&quot;td14&quot;&gt;
        7 &amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp 8&lt;br&gt;&lt;br&gt;9 &amp;nbsp &amp;nbsp&amp;nbsp 0
      &lt;/td&gt;

      &lt;td class=&quot;td15&quot;&gt;
        y &amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp z&lt;br&gt;&lt;br&gt;} &amp;nbsp&amp;nbsp&amp;nbsp %
      &lt;/td&gt;

      &lt;td class=&quot;td16&quot;&gt;
        ^ &amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp [&lt;br&gt;&lt;br&gt;: &amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp ;
      &lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td class=&quot;td17&quot;&gt;
        Q &amp;nbsp&amp;nbsp R&lt;br&gt;&lt;br&gt;S &amp;nbsp &amp;nbsp&amp;nbsp T
      &lt;/td&gt;

      &lt;td class=&quot;td18&quot;&gt;
        * &amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp #&lt;br&gt;&lt;br&gt;@ &amp;nbsp&amp;nbsp&amp;nbsp )
      &lt;/td&gt;

      &lt;td class=&quot;td19&quot;&gt;
        q &amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp r&lt;br&gt;&lt;br&gt;s &amp;nbsp &amp;nbsp&amp;nbsp t
      &lt;/td&gt;

      &lt;td class=&quot;td20&quot;&gt;
        u &amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp v&lt;br&gt;&lt;br&gt;w &amp;nbsp&amp;nbsp&amp;nbsp x
      &lt;/td&gt;
    &lt;/tr&gt;

  &lt;/table&gt;

  &lt;div class=&quot;btn1&quot;&gt;

    &lt;button class=&quot;button&quot; type=&quot;button&quot; onclick=&quot;switchColorUp()&quot;&gt;UP&lt;/button&gt;

    &lt;button class=&quot;button1&quot; type=&quot;button&quot; onclick=&quot;switchColorLeft()&quot;&gt;LEFT&lt;/button&gt;

    &lt;button class=&quot;button2&quot; type=&quot;button&quot; onclick=&quot;switchColorRight()&quot;&gt;RIGHT&lt;/button&gt;

    &lt;button class=&quot;button3&quot; type=&quot;button&quot; onclick=&quot;switchColorDown()&quot;&gt;DOWN&lt;/button&gt;
    &lt;div class=&quot;name&quot;&gt;
      &lt;p&gt;&lt;u&gt;Button To Switch Color Between Boxes:-&lt;/u&gt;&lt;/p&gt;
    &lt;/div&gt;
    &lt;div class=&quot;pass&quot;&gt;
      &lt;u&gt;Password:-&lt;/u&gt;&lt;br&gt;&lt;br&gt;&lt;input size=&quot;40&quot; type=&quot;password&quot; style=&quot;border:1px solid deepskyblue;background-c
olor:black;color:deepskyblue;&quot; name=&quot;User_password&quot;&gt;
    &lt;/div&gt;

    &lt;button class=&quot;butt&quot; type=&quot;button&quot;&gt;.&lt;/button&gt;

    &lt;button class=&quot;butt1&quot; type=&quot;button&quot;&gt;.&lt;/button&gt;

    &lt;button class=&quot;butt2&quot; type=&quot;button&quot;&gt;.&lt;/button&gt;

    &lt;button class=&quot;butt3&quot; type=&quot;button&quot;&gt;.&lt;/button&gt;

    &lt;div class=&quot;name11&quot;&gt;
      &lt;p&gt;&lt;u&gt;Button To Select Character From Boxes:-&lt;/u&gt;&lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

Hope that helps!

答案2

得分: 0

也许我没有理解你的目标。我不理解在交换相邻行时使用(i+4)%20的用途;而且通过完整遍历数组行,将会覆盖在捕获旧值之前的行,并且(i+4)%20会超出数组的大小。

从你的问题文本来看,第一个代码段似乎将数组值移动,就像你想要移动类别一样。

此外,由于你知道你的表格结构是固定的或包含重复模式,你可以在表格或tr元素上制作使用:nth-child选择器的CSS样式,然后更改表格或tr元素上的样式,所有td元素都会发生变化,而无需进行交换。这里有一个MDN示例,展示了这种样式的用法,但你需要为每种可能的组合制作一组样式。有关示例,请参见第二个代码段。

英文:

Perhaps, I'm not understanding your objective. I don't understand the use of (i+4)%20 when swapping rows that are next to each other; plus running the loop through the full array row will overwrite rows before you capture the old value and (i+4)%20 will exceed the size of the array.

From the text of your question, this first snippet appears to move the array values as you'd like to move the classes.

Also, since you know your table structure is fixed or contains a repeating pattern, you could make css styles on table or tr elements that use the :nth-child selector, and then change the style on the table or tr element and all the td elements will change without the need for this swapping. There is an MDN example of a styling like this, but you'd need to make a set of them for each possible combination. See the second snippet for an illustration.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var allTDs = [],
    div = document.querySelector(&#39;DIV&#39;);
for (let i = 0; i &lt; 10; i++) allTDs[i] = &quot;c&quot; + (i+1);
div.textContent = allTDs.join(&#39;---&#39;);

document.body.addEventListener(&quot;click&quot;, shiftClass, false);

function shiftClass () {
  let e = event.target;
  if ( e.matches(&#39;button.up, button.up *&#39;) ) {
    let last = allTDs.length-1,
        temp = allTDs[0];
    for (var i = 0; i &lt; last; i++) {
      allTDs[i] = allTDs[i+1];
    }
    allTDs[last] = temp;
    div.textContent = allTDs.join(&#39;---&#39;);
  } else if ( e.matches(&#39;button.dn, button.dn *&#39;) ) {
    let last = allTDs.length-1,
        temp = allTDs[last];
    for (var i = last; i &gt; 0; i--) {
      allTDs[i] = allTDs[i-1];
    }
    allTDs[0] = temp;
    div.textContent = allTDs.join(&#39;---&#39;);
  }
}

<!-- language: lang-html -->

&lt;button class=&quot;up&quot;&gt;Up&lt;/button&gt;&lt;button class=&quot;dn&quot;&gt;Down&lt;/button&gt;
&lt;div&gt;&lt;/div&gt;

<!-- end snippet -->

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let classIndex = 1,
    table = document.querySelector(&#39;table&#39;);
document.body.addEventListener(&quot;click&quot;, switchClass, false);

function switchClass () {
  let e = event.target;
  if (e.matches(&#39;button.up, button.up *&#39;)) {
    let oldIndex = classIndex;
    classIndex = classIndex == 3 ? 1 : classIndex+1;     
    table.classList.replace(&#39;type&#39;+oldIndex,&#39;type&#39;+classIndex);
  } else if (e.matches(&#39;button.dn, button.dn *&#39;)) {  
    let oldIndex = classIndex;
    classIndex = classIndex == 1 ? 3 : classIndex-1;     
    table.classList.replace(&#39;type&#39;+oldIndex,&#39;type&#39;+classIndex);
  }
}

<!-- language: lang-css -->

td {
 width: 50px;
 height: 30px;
 border: 1px solid black;
}

table.type1 td:nth-child(1),
table.type2 td:nth-child(2),
table.type3 td:nth-child(3) {
  background-color: rgb(100,100,100);
}

table.type1 td:nth-child(2),
table.type2 td:nth-child(3),
table.type3 td:nth-child(1) {
  background-color: rgb(150,150,150);
}

table.type1 td:nth-child(3),
table.type2 td:nth-child(1),
table.type3 td:nth-child(2) {
  background-color: rgb(200,200,200);
}

<!-- language: lang-html -->

&lt;button class=&quot;up&quot;&gt;Up&lt;/button&gt;&lt;button class=&quot;dn&quot;&gt;Down&lt;/button&gt;
&lt;table class=&quot;type1&quot;&gt;
  &lt;tbody&gt; 
    &lt;tr&gt;
      &lt;td&gt;&lt;/td&gt;
      &lt;td&gt;&lt;/td&gt;
      &lt;td&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;/td&gt;
      &lt;td&gt;&lt;/td&gt;
      &lt;td&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;/td&gt;
      &lt;td&gt;&lt;/td&gt;
      &lt;td&gt;&lt;/td&gt;
    &lt;/tr&gt;    
  &lt;/tbody&gt;
&lt;/table&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月27日 00:04:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573236.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定