Javascript 无法读取null的属性“load”
我正在制作一个代码,每10秒自动刷新一次浏览者计数。问题是,没有输出任何内容,在控制台上,它表示无法读取null的属性“load” 我将此用作我的代码:Javascript 无法读取null的属性“load”,javascript,php,jquery,sql,ajax,Javascript,Php,Jquery,Sql,Ajax,我正在制作一个代码,每10秒自动刷新一次浏览者计数。问题是,没有输出任何内容,在控制台上,它表示无法读取null的属性“load” 我将此用作我的代码: <script type="text/javascript"> var auto_refresh = setInterval( function () { $('#viewers').load('ajax.php?request=viewers&id=19').fadeIn("slow"); }, 10000); // re
<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
有人能帮忙吗?非常感谢 只是猜测一下-很可能您的脚本是在加载DOM之前执行的-因为您使用的是Jquery-您可以这样做-
$(document).ready(function(){
var auto_refresh = setInterval(
function ()
{
$('#viewers').load('ajax.php?request=viewers&id=19').fadeIn("slow");
}, 10000);
});
id为viewers的元素尚不存在。将移动到文档末尾或替换为
$(document).ready(function() {
var auto_refresh = setInterval(
function ()
{
$('#viewers').load('ajax.php?request=viewers&id=19').fadeIn("slow");
}, 10000);
});
未测试,但您可以尝试更改:
$('#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);
}
});
$'viewers'调用似乎返回null。这对于jQuery来说是不正常的,因为在任何情况下它都会返回一个对象,即使它不包含任何匹配的元素。
出于这个原因,我怀疑您在加载jQuery时可能有问题。它可能只是与$sign冲突,由jQuery以外的脚本处理,因此请尝试使用:
jQuery('#viewers').load(/*....*/)
如果这也不起作用,那么可能是jQuery甚至没有加载到您的页面上,所以请检查您的脚本引用。我已经尝试在本地运行了。它对我来说非常好 创建一个单独的php文件并检查它 text.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 } ?>
将函数代码放入$document.readyfunction{};我尝试了下面的方法,它停止了错误,但它没有显示在页面上。它确实有效,所以我的问题是,是什么阻止了对象加载。检查第一篇文章中的url。我明天完成这件事。
<?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 } ?>