【问题标题】:Cannot read property 'load' of null无法读取 null 的属性“负载”
【发布时间】:2014-05-10 08:06:18
【问题描述】:

我正在编写一个代码,其中观众计数每 10 秒自动刷新一次。问题是,什么都没有输出,在控制台上,它说 cannot read property 'load' of null

我将其用作我的代码:

<script type="text/javascript">
var auto_refresh = setInterval(
function ()
{
$('#viewers').load('ajax.php?request=viewers&id=19').fadeIn("slow");
}, 10000); // refresh every 10000 milliseconds
</script>

PHP

<?php
define('IN_MYBB', 1); require "./global.php";
require_once MYBB_ROOT."/inc/class_parser.php"; 


switch ($_GET['request']) {

 case 'viewers':
$urlid = $db->escape_string($_GET['id']); // never trust any user inpit
$result = $db->query("SELECT * FROM ****_streams WHERE id='$urlid'");
$row = $db->fetch_array($result);
    echo "Viewers: ".$row['viewers'];
 break;

 case 'contact':

 break;

 default:

 break;

}
?>

有人可以帮忙吗?非常感谢!

【问题讨论】:

  • 把你的函数代码放入$( document ).ready(function() { });
  • 我已经尝试了以下方法,它停止了错误,但它没有显示在页面上。

标签: javascript php jquery sql ajax


【解决方案1】:

看起来$('#viewers') 调用返回空值。这对于 jQuery 来说是不正常的,因为在每种情况下它都会返回一个对象,即使它不包含任何匹配的元素。 出于这个原因,我怀疑您在加载 jQuery 时可能会遇到问题。这可能只是与 $ 符号的冲突,由 jQuery 以外的其他脚本处理,因此请尝试使用:

jQuery('#viewers').load(/*....*/)

如果这也不起作用,那么可能是您的页面上什至没有加载 jQuery,因此请检查您的脚本引用。

【讨论】:

    【解决方案2】:

    只是猜测-很可能您的脚本在加载DOM之前执行-因为您使用的是Jquery-您可以这样做-

    $(document).ready(function(){
    
    var auto_refresh = setInterval(
    function ()
    {
    $('#viewers').load('ajax.php?request=viewers&id=19').fadeIn("slow");
    }, 10000); 
    
    });
    

    【讨论】:

      【解决方案3】:

      未测试,但您可以尝试更改:

      $('#viewers').load('ajax.php?request=viewers&id=19').fadeIn("slow");
      

      用这个:

      $.ajax({
         url: 'ajax.php',
         type : 'get',
         data : { request : viewers, id : 19},
         success: function(data){
            $('#viewers').hide().html(data).fadeIn('slow')
         },
         error: function(xhr){
             console.log(xhr.responseText);
         }
      });
      

      【讨论】:

        【解决方案4】:

        我已经尝试在本地运行它。对我来说效果很好

        创建一个单独的php文件并检查它!

        文本.php

        <?php
        
        if(isset($_GET['request'])){
            echo "Hello from load";
        }else{
            ?>
        <html>
        <head>
        <script src="jquery.js"></script> <!-- replace with ur jquery -->
        <script type="text/javascript">
        $(function(){
        
        setInterval(
        function ()
        {
        $('#output').load('text.php?request=viewers&id=19').fadeIn("slow");
        }, 10000); 
        
        });
        
        </script>
        </head>
        <body>
        <h2>Content</h2>
        <div id='output'>
        </div>
        </body></html>
        <?php } ?> 
        

        【讨论】:

        • 它确实有效,所以我的问题是,是什么阻止了对象加载。检查第一篇文章中的网址。我明天完成。
        【解决方案5】:

        id 为“viewers”的元素还不存在。将 &lt;script&gt; 移动到文档末尾或将其替换为

        $(document).ready(function() {
            var auto_refresh = setInterval(
            function ()
            {
                 $('#viewers').load('ajax.php?request=viewers&id=19').fadeIn("slow");
            }, 10000);
        });
        

        【讨论】:

          猜你喜欢
          • 2017-10-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-01-29
          • 1970-01-01
          • 2016-11-24
          相关资源
          最近更新 更多