簡體   English   中英

如何隱藏/顯示從PHP生成代碼的HTML元素的可見性?

[英]How to hide / show visibility of HTML elements generating code from PHP?

我需要在代碼中隱藏/顯示HTML元素。 該頁面是用PHP生成的。

在這里嘗試簡化我的代碼,您將展示一個顯示我的麻煩的示例...

echo '<button type="button" onclick="hide_show('.$n_images.')">Hide / show details</button>';

echo '<script  language="javascript">';
echo 'function hide_show(n_images)';
echo '  {';
echo '     element = document.getElementById("details_0");';
echo '     if (element.style.visibility == \'visible\') {';
echo '      element.style.visibility = \'hidden\'';
echo '     }';
echo '     else {';
echo '      element.style.visibility = \'visible\'';
echo '     }';
echo '  }';
echo '</script>';

在這種情況下,代碼可以正常工作了。

我的問題是我有幾個要隱藏/顯示的元素,所以我需要替換該行

echo '     element = document.getElementById("details_0");';

用了一段時間,我不知道概括該代碼...

我的最終代碼應該是這樣的……

echo '<button type="button" onclick="hide_show('.$n_images.')">Hide / show details</button>';

echo '<script  language="javascript">';
echo 'function hide_show(n_images)';
echo '  {';
echo '   while (current_index < n_images )  {';
echo '     element = document.getElementById("details_0");'; --> this is the line I need to generalize !!!
echo '     if (element.style.visibility == \'visible\') {';
echo '      element.style.visibility = \'hidden\'';
echo '     }';
echo '     else {';
echo '      element.style.visibility = \'visible\'';
echo '     }';
echo '     current_index = current_index + 1;';
echo '   }';
echo '  }';
echo '</script>';

有幫助/建議/示例嗎?

先感謝您!

切薩雷

echo '<button type="button" onclick="hide_show('.$n_images.')">Hide / show details</button>';
echo '<script  language="javascript">';
echo 'var i = 0';
echo 'function hide_show(n_images)';
echo '  {';
echo '   while (current_index < n_images )  {';
echo '     element = document.getElementById("details_" + i);'; --> this is the line I need to generalize !!!
echo '     if (element.style.visibility == \'visible\') {';
echo '      element.style.visibility = \'hidden\'';
echo '     }';
echo '     else {';
echo '      element.style.visibility = \'visible\'';
echo '     }';
echo '     current_index = current_index + 1;';
echo '     i++;';   //increment variable i value...
echo '   }';
echo '  }';
echo '</script>';

只需初始化計數器,例如“ var i”,然后將其添加到ever id。

嗨,Cesare使您變得更輕松,為什么不只對整個JavaScript使用一個php echo語句。 在您的while循環之前,將current_index初始化為0。在您的js行中,您需要進行概括的行getElementById(“ details_” + current_index)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM