Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquery函数没有';我什么也没表现出来_Javascript_Jquery - Fatal编程技术网

Javascript jquery函数没有';我什么也没表现出来

Javascript jquery函数没有';我什么也没表现出来,javascript,jquery,Javascript,Jquery,我正在尝试创建一个随机生成器,如果在另一个页面上发现一个提示,使用jQuery将很容易,所以我尝试了以下方法 <html> <head> <title>hello</title> </head> <body> <script type="text/javascript"> $ (document).ready(function() { $("body").load("hello.txt

我正在尝试创建一个随机生成器,如果在另一个页面上发现一个提示,使用jQuery将很容易,所以我尝试了以下方法

<html>
 <head>
  <title>hello</title>
 </head>
 <body>
  <script type="text/javascript">
   $ (document).ready(function() {
    $("body").load("hello.txt", function(msg) {
        var textArray = msg.split("\n");
    var zufall = Math.round ( ( textArray.length - 1 ) * ( Math.random() ) );
    });
   });
   document.write('<p>' + textArray[zufall] + '</p>');
  </script>
 </body>
</html>

你好
$(文档).ready(函数(){
$(“body”).load(“hello.txt”,函数(msg){
var textary=msg.split(“\n”);
var zufall=Math.round((textArray.length-1)*(Math.random());
});
});
document.write(''+textArray[zufall]+'

');
它应该是这样工作的: 它加载包含多行文本的文档,并在换行时将其拆分。这应该存储在一个数组中,并在网站上显示一条随机线

我的第一个想法是将文本直接写入数组,但我认为加载它对网站来说会更有效

谢谢你的回答

PS:当浏览器运行时,不会出现类似“此页出错”的错误消息

最终编辑

谢谢你的帮助!!! 现在它起作用了

以下是解决方案:

<html>
    <head>
        <title>hello</title>
    </head>
    <body>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
            <script type="text/javascript">
            $ (document).ready(function() {
                 $.get("hello.txt", function(msg) {
                    var textArray = msg.split("\n");
            var zufall = Math.round ( ( textArray.length - 1 ) * ( Math.random() ) );

            $('body').append('<p>' + textArray[zufall] + '</p>');
                });
            });
        </script>
    </body>
</html>

你好
$(文档).ready(函数(){
$.get(“hello.txt”,函数(msg){
var textary=msg.split(“\n”);
var zufall=Math.round((textArray.length-1)*(Math.random());
$('body').append(''+textArray[zufall]+'

'); }); });
您需要将
document.write()
放入
函数(msg)
中,因为AJAX是异步的,
load
正在使用AJAX,所以
document.write()
不会等到
load
完成匿名函数的调用

   $(document).ready(function() {
    $.get("hello.txt", function(msg) {
        var textArray = msg.split("\n");
        var zufall = Math.round ( ( textArray.length - 1 ) * ( Math.random() ) );
        $('body').append('<p>' + textArray[zufall] + '</p>');
    });
   });
$(文档).ready(函数(){
$.get(“hello.txt”,函数(msg){
var textary=msg.split(“\n”);
var zufall=Math.round((textArray.length-1)*(Math.random());
$('body').append(''+textArray[zufall]+'

'); }); });
编辑:

我刚刚注意到您没有包括jquery库o_o

在您的


NiftyDude将document.write调用置于document.ready函数的范围之外是正确的。此外:

  • 使用document.write几乎总是一个坏主意。在本例中,您正在等待AJAX调用在使用它之前完成,这意味着您保证document.write将覆盖整个页面正文
  • 您正在使用$('body').load,这在本例中是不合适的--您将手动向正文中添加文本
这里有一个解决方案:

<html>
 <head>
  <title>hello</title>
 </head>
 <body>>
  <script type="text/javascript" src="jquery.js"></script>
  <script type="text/javascript">
   $(document).ready(function() {
    $.get("hello.txt", function(msg) {
        var textArray = msg.split("\n");
        var zufall = Math.round ( ( textArray.length - 1 ) * ( Math.random() ) );
        $('body').append('<p>' + textArray[zufall] + '</p>');
    });
   });
  </script>
 </body>
</html>

你好
>
$(文档).ready(函数(){
$.get(“hello.txt”,函数(msg){
var textary=msg.split(“\n”);
var zufall=Math.round((textArray.length-1)*(Math.random());
$('body').append(''+textArray[zufall]+'

'); }); });
尝试此替代解决方案

$(document).ready(function() {
    $.ajax({ 
        url: "hello.txt",
        type: "GET",
        dataType: "text",
        success: function(data) {
            textArray = data.split("\n");
            zufall = Math.round ( ( textArray.length - 1 ) * ( Math.random() ) );
             document.write('<p>' + textArray[zufall] + '</p>');
        }
    });
});
$(文档).ready(函数(){
$.ajax({
url:“hello.txt”,
键入:“获取”,
数据类型:“文本”,
成功:功能(数据){
textArray=data.split(“\n”);
zufall=Math.round((textArray.length-1)*(Math.random());
document.write(''+textArray[zufall]+'

'); } }); });

还要确保包含了jquery文件。

您没有包含jquery文件。您是指这样的代码行吗?是的,如果jquery.js文件位于同一目录中,那么jquery文件究竟在哪里呢?类似这样的情况:您不应该在回调中使用document.write。如果在DOM完成加载后执行document.write,它将覆盖整个页面。感谢您的提示!!它现在速度更快,工作正常,但现在它首先打印整个数组。听起来您可能没有用$.get替换$('body').load。
$(document).ready(function() {
    $.ajax({ 
        url: "hello.txt",
        type: "GET",
        dataType: "text",
        success: function(data) {
            textArray = data.split("\n");
            zufall = Math.round ( ( textArray.length - 1 ) * ( Math.random() ) );
             document.write('<p>' + textArray[zufall] + '</p>');
        }
    });
});