当我单击日期值时出现此“错误
未捕获的TypeError:无法读取未定义的属性“ textContent”

这是我的代码,您能帮我确定错误的根源吗,我该如何纠正呢?

<?php
while($row = mysqli_fetch_array($query)){
                                    $date = $row['Date'];
                                    $time = $row['Time'];
                                    $latitude= $row['Latitude'];
                                    $longitude= $row['Longitude'];
                                    $depth =$row['Depth'];
                                    $magnitude = $row['Magnitude'];
                                    //$array_lat_lon[] = $lat = $row['LAT'];
                                    //$array_lat_lon[] = $lon = $row['LON'];

                                $the_arraypei[] = array($row['Date'] ); //added
                                //$the_array[] = array($row['LAT']."" , "".$row['LON']) ;
                                //$timestamp = strtotime()
                                echo '<tr class="normalRow"><td id="date2"><a href="#" onClick="functiontoget(\'$date\');">'.$date.'</a></td><td border="1">'.$time.'</td><td border="1">'.$latitude.'</td><td border="1">'.$longitude.'</td><td border="1">'.$depth.'</td><td border="1">'.$magnitude.'</td></tr>';


                                                                }?>

   <script>
     function functiontoget($date) {
     var x = document.getElementsByTagName("#date")[0].textContent;
     document.getElementById("demo").innerHTML = x;}
     </script>
     <p id="demo"></p>

最佳答案

如果要按标签名称查询元素,可以使用getElementsByTagName,但是是否有一个名为#date的标签? #date是一个元素的id,您需要使用getElementById

更改您的JavaScript代码:

function functiontoget($date) {
   var x = document.getElementById("date").textContent;
   document.getElementById("demo").innerHTML = x;
}

关于javascript - 未捕获的TypeError:无法读取未定义的属性'textContent',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39384937/

10-10 02:54