简体   繁体   English

如何为多个div元素循环相同的javascript代码?

[英]How to loop same javascript code for more than one div elements?

I have made three "boxes" and each box contains a button. 我做了三个“盒子”,每个盒子都包含一个按钮。 When I click the button, box hiding, when click again, box appears. 当我单击按钮时,隐藏框,再次单击时,出现框。

This is my html code: 这是我的html代码:

<div id="SC1_A_"> <!-- BOX -->
<div id="SC1_B_" onClick="SC1();" class="something">&nbsp;</div> <!-- BUTTON -->
</div>
<div id="SC2_A_">
<div id="SC2_B_" onClick="SC2();" class="something">&nbsp;</div>
</div>
<div id="SC3_A_">
<div id="SC3_B_" onClick="SC3();" class="something">&nbsp;</div>
</div>

This is my javascript code: 这是我的JavaScript代码:

<script type="text/javascript">    
function SC1(){

  var SC1_A = document.getElementById('SC1_A_);
  var SC1_B = document.getElementById('SC1_B_);

  if (SC1_A.style.display == 'block' || SC1_A.style.display == ''){
      SC1_A.className      = 'something';
      SC1_B.className      = 'something else';}   
else {SC1_A.className      = 'something else';
      SC1_B.className      = 'something';}
     }
     }
</script>

The example above works, but I have to make three similar scripts for each button. 上面的示例有效,但是我必须为每个按钮制作三个类似的脚本。 So I though to make something like this script below, using for loop. 因此,尽管我在下面使用for循环来制作类似此脚本的内容。 As you can imagine it didn't work. 您可以想象它没有用。 Any idea how can I make it work??? 你知道我该怎么做吗???

<script type="text/javascript">

for (i=1; i<10; i++){

function SCi(){

  var SCi_A = document.getElementById('SC'+i+'_A_');
  var SCi_B = document.getElementById('SC'+i+'_B_');

  if (SCi_A.style.display == 'block' || SCi_A.style.display == ''){
      SCi_A.className      = 'something';
      SCi_B.className      = 'something else';}   
else {SCi_A.className      = 'something else';
      SCi_B.className      = 'something';}
     }
     }
</script>

Please don't down-vote if you think question is too easy, but just give me your help here!!! 如果您认为问题太简单了,请不要投票,但请在这里给我您的帮助!!! Thank you in advance!!! 先感谢您!!!

You're on the right track, you just need to learn the right syntax for what you are trying to express: 您走的路正确,您只需要学习要表达的内容的正确语法即可:

var SC = [];

First off, to have a lot of different functions, so instead of attempting to name them differently (which you were trying to do), we are going to just store each function in a different index in the SC array. 首先,要具有很多不同的功能,因此,我们将尝试将每个功能存储在SC数组中的不同索引中,而不是尝试使用不同的名称命名(您尝试这样做)。

for (var i = 1; i < 10; i++) {
    SC[i] = (function () {
        var SC_A = document.getElementById('SC' + i + '_A_');
        var SC_B = document.getElementById('SC' + i + '_B_');

        return function () {
            if (SC_A.style.display === 'block' || SC_A.style.display === '') {
                SC_A.className = 'something';
                SC_B.className = 'something else';
            } else {
                SC_A.className = 'something else';
                SC_B.className = 'something';
            }
        }
    })();
}

Now, to call these functions you would do SC[1]() , SC[2]() , ... So you can either put that in each onclick in your HTML, or you could bind the events from the javascript. 现在,要调用这些函数,您需要执行SC[1]()SC[2]() ,...,因此您可以将其放在HTML的每次onclick中,也可以绑定来自javascript的事件。


Edit: I forgot to mention this because it isn't directly related to the syntax of the code, but the calls to 'document.getElementById will not work until the document is fully loaded. So if you just put the script directly between to 编辑:我忘了提及这一点,因为它与代码的语法没有直接关系,但是will not work until the document is fully loaded. So if you just put the script directly between to对'document.getElementById的调用will not work until the document is fully loaded. So if you just put the script directly between to will not work until the document is fully loaded. So if you just put the script directly between to ` tags it won't work. will not work until the document is fully loaded. So if you just put the script directly between to `标签will not work until the document is fully loaded. So if you just put the script directly between to则将无法使用。 You have two choices. 您有两种选择。 You either can keep the current code, but run it when the page loads . 您可以保留当前代码,但可以在页面加载时运行它 Or, you could restructure the code like this: 或者,您可以像这样重组代码:

var SC = [];
for (var i = 1; i < 10; i++) {
    SC[i] = (function (i) {
        return function () {
            var SC_A = document.getElementById('SC' + i + '_A_');
            var SC_B = document.getElementById('SC' + i + '_B_');

            if (SC_A.style.display === 'block' || SC_A.style.display === '') {
                SC_A.className = 'something';
                SC_B.className = 'something else';
            } else {
                SC_A.className = 'something else';
                SC_B.className = 'something';
            }
        }
    })(i);
}

What's happening here is you are calling document.getElementById every time the button is clicked, instead of just once when the function is created. 这里发生的事情是您每次单击按钮时都调用document.getElementById ,而不是在创建函数时调用一次。 Slightly less efficient, but it works. 效率稍低,但可以。

You define each section on the page as calling the one function and passing in the name of the other . 您将页面上的每个部分定义为调用一个函数并传入另一个函数的名称。

<div id="SC1_A_"> <!-- BOX -->
<div id="SC1_B_" onClick="SC('SC1_A_');" class="something">&nbsp;</div> <!-- BUTTON -->
</div>
<div id="SC2_A_">
<div id="SC2_B_" onClick="SC('SC2_A_');" class="something">&nbsp;</div>
</div>
<div id="SC3_A_">
<div id="SC3_B_" onClick="SC('SC3_A_');" class="something">&nbsp;</div>
</div>

There is just one function used for all of them 只有一种功能可用于所有这些功能

function SC(nameOfA){

   var SCi_A = document.getElementById(nameOfA);
   var SCi_B = this;

   if (SCi_A.style.display == 'block' || SCi_A.style.display == ''){
       SCi_A.className      = 'something';
       SCi_B.className      = 'something else';
   } else {
       SCi_A.className      = 'something else';
       SCi_B.className      = 'something';}
   }
}

here you can use this function on every click: 在这里,您可以在每次点击时使用此功能:

    <div id="SC1_A_"> <!-- BOX -->
         <div id="SC1_B_" onClick="SC(event)" class="something">&nbsp;</div> <!-- BUTTON -->
    </div>
<script type="text/javascript">
    function SC(event){
        var SCA = event.currentTarget.parentNode;
        var SCB = event.currentTarget;

        ................
    }
</script>

Your code is defining a function named SCi 8 times. 您的代码定义了8次名为SCi的函数。 I think if you swap the first two lines you will get what you want. 我认为,如果您交换前两行,将会得到您想要的。

You're redefining the same function ( function SCi ) eight times. 您将八次重新定义相同的功能( function SCi )。 The only version of the function that is retained is the version that's defined last. 保留的唯一函数版本是最后定义的版本。 Going by your code, you're only creating a function that can work with the 8th box. 按照您的代码,您只创建了一个可以与第8个框一起使用的函数。

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

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