dongpu9852 2018-03-26 12:35
浏览 97
已采纳

innerHTML的内容是“未定义的”

I never had this problem before, but now working with PHP when I try to edit the content of a div with the id of a product taken directly from it's id in the database like this (both separaed in two foreach getting their current correct IDs) because I need them separated so when I can change the content, I can modify it so I can make the second DIV display: none by default and after clicking the first one making it display: inline:

<div id="h<?php echo $obra['id'] ?>" onClick="display()"> </div> // Getting ID h + 84 (h84) this time
<div id="<?php echo $obra['id'] ?>"> TEST TEXT</div> // Getting ID 84 this time)

And the function is:

function display() {
    var result = document.getElementById("84").innerHTML;
    result.innerHTML = "test";
    alert(result);
}

Now, when I click the first DIV, it should get the content of the div with ID 84, which is "TEST TEXT", and then change it for "test" and change the content which I see in the browser and after that, alert with the new result value, but the content doesn't change and the alert shows me TEST TEXT.

Here is the full relevant code if needed:

<div class="row m-0">
            <div class="accordion pl-0 col-4 text-center">
        <?php   if ( count($cuadros) == 0) { ?>
                    <p class="text-center mt-3">No hay cuadros registrados.</p>
        <?php   } else { 
                    $cont = 0;
                        foreach ( $cuadros as $obra ) { ?>
                            <div class="card border-top-0 border-left-0 rounded-0 p-0">
                                <div class="card-header border-0" id="h<?php echo $obra['id'] /* it gets value "84" */ ?>" onClick="display()">
                                    <h5 class="mb-0">
        <?php                           echo $obra['nombreObras']; ?>
                                    </h5>
                                </div>
                            </div>
        <?php       $cont++; } ?>
            </div>
        <?php foreach ( $cuadros as $obra ) { ?>
            <div class="col-4 hidden" id="<?php echo $obra['id'] /* It gets value "84" */ ?>">
                TEST TEXT
            </div>
        <?php   } ?>
        <?php } ?>
        </div>

And a screenshot of what happens (note that the change should be reflected in the alert, which is not) Image

Thank you!

  • 写回答

1条回答 默认 最新

  • 普通网友 2018-03-26 12:38
    关注

    You are performing innerHTML agin. So it will return error. Remove innerHTML from result

    function display() {
        var result = document.getElementById("84");
        result.innerHTML = "test";
        alert(result);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线