简体   繁体   English

php循环的Javascript函数

[英]Javascript function for a php loop

I have this situation: 我有这种情况:

<script type="text/javascript">
function myFunction() {
    var x = document.getElementById("commentDIV");
    if (x.style.display === "none") {
        x.style.display = "block";
    } else {
        x.style.display = "none";
    }
}
</script>

and the php code 和PHP代码

<?php
echo "<table><tr  onclick='myFunction()'><td>Row A1</td></tr>"; //A1 row from my table
echo "<tr><td><div id='commentDIV' style='display:none'> Row B1</div></td></tr></table>"; // the B1 row show after click on the A1
?>

Everything works fine, when I click on the first row, the second appearance. 当我单击第一行,第二个外观时,一切正常。

How can I use/modify my javascript function in this situation? 在这种情况下,如何使用/修改我的javascript函数?

<?php
    $x=0;
    while($x<10){
        echo "<table><tr  onclick='myFunction()'><td>Row A$x</td></tr>"; //Ax row from my table
        echo "<tr><td><div id='commentDIV' style='display:none'> Row B$x</div></td></tr></table>"; // the Bx row must show after click on the Ax
        $x++;
    }
?>

Please help and save my day! 请帮助并保存我的一天! Many thanks! 非常感谢!

The problem is that you are using the same id "commentDIV" for all Divs so you should change the loop and function. 问题在于,所有Div都使用相同的ID“ commentDIV”,因此您应该更改循环和功能。

  1. Add $x to div id originally known as commentDIV $x添加到最初称为commentDIV div id
  2. Change the myFunction call to include the $x number 更改myFunction调用以包括$x
  3. Add the id argument to the myFunction definition 将id参数添加到myFunction定义中
  4. Append the passed id to the getElementById value 将传递的ID附加到getElementById

Like this: 像这样:

<?php 
$x=0;
while($x<10){
  echo "<table><tr  onclick='myFunction($x)'><td>Row A$x</td></tr>";
  //                                    ^^--- 2. add $x
  echo "<tr><td><div id='commentDIV_$x' style='display:none'> Row B$x</div></td></tr></table>";
  //                               ^^^--- 1. add $x
$x++;}
?>

The javascript code changes to this: javascript代码更改为:

<script type="text/javascript">
    function myFunction(id) {
         //             ^^--- 3. add id
         var x = document.getElementById("commentDIV_" + id);
         //                                         ^ ^^^^^--- 4. append id
         if (x.style.display === "none") {
            x.style.display = "block";
         } else {
            x.style.display = "none";
         }
    }
</script>

id attribute should be unique at the document, try to change the duplicate ones by common classes like : id属性在文档中应该是唯一的,请尝试通过常见的类更改重复的属性,例如:

<?php
    $x=0;
    while($x<10){
        echo "<table><tr  onclick='myFunction(this)'><td>Row A$x</td></tr>"; //Ax row from my table
        echo "<tr><td><div class='commentDIV' style='display:none'> Row B$x</div></td></tr></table>"; // the Bx row must show after click on the Ax
        $x++;
    }
?>

NOTE: You should pass the 'this' object as an argument to your function. 注意:您应该将“ this”对象作为参数传递给函数。

Then in your js, you could use the passed argument to search for the related div using the class name self.nextElementSibling.querySelector(".commentDIV") and finally toggle the display like : 然后在您的js中,您可以使用传递的参数使用类名称self.nextElementSibling.querySelector(".commentDIV")搜索相关的div ,最后像下面这样切换显示:

 function myFunction(self) { var related_div = self.nextElementSibling.querySelector(".commentDIV"); related_div.style.display = (related_div.style.display === 'none') ? 'block' : 'none' } 
 <table border=1> <tr onclick='myFunction(this)'> <td>Row A1</td> </tr> <tr> <td> <div class='commentDIV' style='display:none'> Row B1</div> </td> </tr> <tr onclick='myFunction(this)'> <td>Row A2</td> </tr> <tr> <td> <div class='commentDIV' style='display:none'> Row B2</div> </td> </tr> </table> 

First of all, IDs need to be unique to function properly, so I would rather use classes. 首先,ID必须是唯一的才能正常运行,所以我宁愿使用类。

As stated, this table will be echoed 10 times: 如前所述,该表将被回显10次:

<table>
  <tr onclick="myFunction(this)">
    <td>Row A$x</td>
  </tr>
  <tr>
    <td>
      <div class="commentDIV" style="display:none">Row B$x</div>
    </td>
  </tr>
</table>

Next, modify the javascript such that the element gets passed as a parameter, then get the first child of the next element: 接下来,修改javascript,以便将该元素作为参数传递,然后获取下一个元素的第一个子元素:

function myFunction(e) {
  var x = e.nextElementSibling.children[0];
  if (x.style.display == "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}

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

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