Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.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 警报(reponseText&x2B;$(.class))_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 警报(reponseText&x2B;$(.class))

Javascript 警报(reponseText&x2B;$(.class)),javascript,jquery,ajax,Javascript,Jquery,Ajax,我要打印Hello World。但它不起作用。为什么?问题是在第30行,我如何更改打印“Hello World”的这一行。按类“示例”?我已经尝试过varx=y.getElementsByClassName(“示例”)和var x=variavelhtttp.responseXML.getElementsByClassName(“示例”)但没有成功 index.html <html xmlns="http://www.w3.org/1999/xhtml"> <head>

我要打印Hello World。但它不起作用。为什么?问题是在第30行,我如何更改打印“Hello World”的这一行。按类“示例”?我已经尝试过var
x=y.getElementsByClassName(“示例”)
var x=variavelhtttp.responseXML.getElementsByClassName(“示例”)但没有成功

index.html

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <title>Load page</title>

    <script src="https://code.jquery.com/jquery-1.10.2.js"></script>

    <script type="text/javascript">
    if(window.XMLHttpRequest){
    variavelhtttp = new XMLHttpRequest();
    }else{
    alert("Withouth Ajax!");
    }
    </script>

</head>

<body>

    <button type="button" onclick="loadDoc()">Go</button>

    <script type="text/javascript">
        function loadDoc(){
            variavelhtttp= new XMLHttpRequest();
            variavelhtttp.open("GET","text.html",false);
            variavelhtttp.send();
            var y = variavelhtttp.responseText;
            window.alert(y);
            var x = $( ".example" ).html(y);
            alert(x);
         }
    </script>

</body>
</html>

加载页
if(window.XMLHttpRequest){
variavelhtttp=newXMLHttpRequest();
}否则{
警报(“没有Ajax!”);
}
去
函数loadDoc(){
variavelhtttp=newXMLHttpRequest();
variavelhtttp.open(“GET”,“text.html”,false);
variavelhtttp.send();
变量y=变量Http.responseText;
窗口警报(y);
var x=$(“.example”).html(y);
警报(x);
}
text.html

<html>
<head>
    <title></title>
</head>
<body>
    <div class="example">Hello World.</div>
</body>
</html>

你好,世界。
$(“.example”).html(y)
在其中设置值y。example

var x=$(“.example”).html(y)将使x成为一个对象,正如您在
警报(x)中看到的那样

您想要的警报:

alert(  $( ".example" ).html() );

$(“.example”).html(y)
在其中设置值y。example

var x=$(“.example”).html(y)将使x成为一个对象,正如您在
警报(x)中看到的那样

您想要的警报:

alert(  $( ".example" ).html() );


@snoopy,如果要访问从axaj调用检索的div.example的内部文本,必须正确解析它。首先,如果您使用的是jQuery,那么在这里使用vanillaJS毫无意义。其次,您的HTTPRequest不是ajax调用,因为您在同步模式下使用它(而不是异步模式,ajax上的A代表异步模式)

下面是一个使用
load
和过滤器的示例

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Load page</title>
</head>

<body>

    <button type="button">Go</button>

    <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
    <script type="text/javascript">
        $('button').click(loadDoc);

        function loadDoc(){
            //use a DOM element created by jQuery to hold the response! 
            var container = $('<div>'); 
            //.load allows you to filter the text using a CSS selector
            container.load("text.html .example", null, print);

            //uses the complete handler to execute code after ajax done
            function print(){
              var example = $('.example', container);
              alert(example.text());
              //you can ommit this line if you don't want to append in the real DOM
              $('body').append( example );
            }
         }
    </script>

</body>
</html>

加载页
去
$(“按钮”)。单击(loadDoc);
函数loadDoc(){
//使用jQuery创建的DOM元素来保存响应!
变量容器=$('');
//.load允许您使用CSS选择器筛选文本
container.load(“text.html.example”,null,print);
//使用完整的处理程序在ajax完成后执行代码
函数打印(){
变量示例=$('.example',容器);
警报(例如.text());
//如果不想在真正的DOM中追加,可以使用ommit命令行
$('body')。追加(示例);
}
}
实例


仅使用HTML和vanillaJS的实时示例:

@snoopy,如果要访问从axaj调用检索的div.example的内部文本,必须正确解析它。首先,如果您使用的是jQuery,那么在这里使用vanillaJS毫无意义。其次,您的HTTPRequest不是ajax调用,因为您在同步模式下使用它(而不是异步模式,ajax上的A代表异步模式)

下面是一个使用
load
和过滤器的示例

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Load page</title>
</head>

<body>

    <button type="button">Go</button>

    <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
    <script type="text/javascript">
        $('button').click(loadDoc);

        function loadDoc(){
            //use a DOM element created by jQuery to hold the response! 
            var container = $('<div>'); 
            //.load allows you to filter the text using a CSS selector
            container.load("text.html .example", null, print);

            //uses the complete handler to execute code after ajax done
            function print(){
              var example = $('.example', container);
              alert(example.text());
              //you can ommit this line if you don't want to append in the real DOM
              $('body').append( example );
            }
         }
    </script>

</body>
</html>

加载页
去
$(“按钮”)。单击(loadDoc);
函数loadDoc(){
//使用jQuery创建的DOM元素来保存响应!
变量容器=$('');
//.load允许您使用CSS选择器筛选文本
container.load(“text.html.example”,null,print);
//使用完整的处理程序在ajax完成后执行代码
函数打印(){
变量示例=$('.example',容器);
警报(例如.text());
//如果不想在真正的DOM中追加,可以使用ommit命令行
$('body')。追加(示例);
}
}
实例


仅使用HTML和vanillaJS的实时示例:

您希望看到什么?问题是什么?为什么要在jQuery中使用纯JavaScript?您可以使用HTML并使用jQuery发出ajax请求…
$(“.example”).text()这将为您提供内容。您希望得到什么?问题是什么?为什么要在jQuery中使用纯JavaScript?您可以使用HTML并使用jQuery发出ajax请求…
$(“.example”).text()这将为您提供内容。哦,明白了!因此只需要更改警报(x);用于警报($(“.example”).html());或警报($(“.example”).text());?是,或者在var x=…行中将(y)更改为()。。。我不知道关于httprequest的事。因此,请遵循其他人的建议。哦,明白了!因此只需要更改警报(x);用于警报($(“.example”).html());或警报($(“.example”).text());?是,或者在var x=…行中将(y)更改为()。。。我不知道关于httprequest的事。因此,请遵循其他优秀工作的建议!我完全理解。非常感谢,干得好!我完全理解。非常感谢你。