2011-02-28 37 views
1

我得到一个“没有定义功能”的JavaScript错误与下面的代码,它使用jQuery的:错误的脚本W“没有定义功能”/jQuery的

<!DOCTYPE html> 
<html> 

<head> 

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script> 

<script type="text/javascript"> 

function log_in() { 
    document.write('Logging in ... '); 
    $.post('process_ajax.php', { 'action' : 'log in' }, function(data) { 
     document.write(data); 
     create_new_story(); 
    }); 
} 

function create_new_story() { 
    document.write('Creating new story ... '); 
    $.post('process_ajax.php', { 'action' : 'create story' }, function(data) { 
    document.write(data); 
    }); 
} 

</script> 

</head> 

<body onload="log_in()"> 
Processing...<br> 
</body> 

</html> 

这里的PHP脚本,它调用(用于测试目的):

<? 

die("Page opened.<br>"); 

?> 

调用的第一个功能 - log_in() - 工作正常,但我得到这个错误:

“错误:create_new_story没有定义”

而且这个函数永远不会被执行。

我确定我缺少一些简单的东西。我能找到一双新的眼睛来找到它吗?

回答

4

您只能在页面仍在载入时调用document.write

您不能在以后调用它,否则会吹掉现有页面。

相反,您应该使用jQuery的.append方法。

0

在DOM准备好之后调用document.write()时,它实际上用document.write()的参数替换整个页面。

参见DEMO