简体   繁体   English

JavaScript不会显示/隐藏div-使用PHP / MySQL

[英]JavaScript won't show/hide div - working with PHP/MySQL

In my PHP/MySQL while loop when selecting data for an activity feed, I'm trying to show comments underneath each post so that when you click "show", it shows all of the comments. 在我的PHP / MySQL while循环中,为活动供稿选择数据时,我试图在每个帖子下方显示注释,以便在单击“显示”时显示所有注释。

I'm using the following code in the while loop (so this is dynamically displayed numerous times for each separate update): 我在while循环中使用以下代码(因此,对于每个单独的更新,此代码会动态显示多次):

<script language="javascript"> 
function toggle' . $act_item_id . '() {
    var ele = document.getElementById("toggleText' . $act_item_id . '");
    var text = document.getElementById("displayText' . $act_item_id . '");
    if(ele.style.display == "block") {
            ele.style.display = "none";
        text.innerHTML = "show";
    }
    else {
        ele.style.display = "block";
        text.innerHTML = "hide";
    }
} 
</script>

<a id="displayText' . $act_item_id . '" href="javascript:toggle' . $act_item_id . '();">show</a>
<div id="toggleText' . $act_item_id . '" style="display: none">' . $responseList . '</div>

' . '。 $act_item_id . $ act_item_id。 ' contains the ID of the update, making everything unique. '包含更新的ID,使所有内容唯一。

When you click show, the JavaScript doesn't show the div. 当您单击显示时,JavaScript不会显示div。

FYI: Content is loaded into another page via an AJAX call. 仅供参考:通过AJAX调用将内容加载到另一个页面中。

AJAX call is as follows: AJAX调用如下:

function list_activity(){
    var hr = new XMLHttpRequest();
    hr.onreadystatechange = function(){
    if (hr.readyState==4 && hr.status==200){
    document.getElementById("viewActivity").innerHTML = hr.responseText;
    }
    }
    hr.open("GET", "listActivity.php?t=" + Math.random(), true);
    hr.send();
}

I'm not entirely sure you can create and call JS functions like that. 我不完全确定您可以像这样创建和调用JS函数。 Besides, I don't see any reason to re-create the function for each section. 此外,我看不出有任何理由为每个部分重新创建函数。 Why don't you simply parameterize you function and change the parameter each time through? 为什么不简单地对函数进行参数化并每次更改参数呢?

Define function once 一次定义功能

<script>
function toggle(act_item_id) {
    var ele = document.getElementById("toggleText' . act_item_id . '");
    var text = document.getElementById("displayText' . act_item_id . '");
    if(ele.style.display == "block") {
        ele.style.display = "none";
        text.innerHTML = "show";
    }
    else {
        ele.style.display = "block";
        text.innerHTML = "hide";
    }
} 
</script>

Then you can loop through 然后你可以循环通过

while (some condition) {
    ...
    <a id="displayText' . $act_item_id . '" href="javascript:toggle('.$act_item_id.');">show</a>
    <div id="toggleText' . $act_item_id . '" style="display: none">' . $responseList . '</div>
    ...
}

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

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