Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.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,为什么这段代码输出4,而所有其他内容都没有输出 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-T

为什么这段代码输出4,而所有其他内容都没有输出

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>jQuery</title>
    <script src="jquery-1.9.1.js"></script>
    <script>
    $(document).ready(function() {
      var par = $("p");
      document.write(par.length);
    });
    </script>
</head>

<body>
    <p>I am one</p>
    <p>I am two</p>
    <p>I am three</p>
    <p>I am four</p>
</body>

</html>

jQuery
$(文档).ready(函数(){
VaR=$(“p”);
文件书写(标准长度);
});
我就是其中之一

我两岁

我三岁了

我四岁了


在文档准备就绪后调用document.write时,它会删除文档中的所有内容,因为它会调用document.open,从而清除所有内容

看起来您想要的只是将
par.length
附加到正文中

$(document).ready(function() {

    var par = $("p");
    $('body').append(par.length);

});

在文档准备就绪后调用document.write时,它会从文档中删除所有内容,因为它调用document.open,这会清除所有内容

看起来您想要的只是将
par.length
附加到正文中

$(document).ready(function() {

    var par = $("p");
    $('body').append(par.length);

});

当文档准备好时,您已经执行了脚本-很好。但是document.write()将直接写入现有文档,替换其中的内容,因此段落将消失

由于您使用的是jQuery,因此最好让jQuery为您添加项:

$("body").append("<p>Paragraph count:"+$("p").length+"</p>");
$(“body”).append(段落计数:“++$(“p”).length+”

”);
当文档准备好时,您已经执行了脚本-很好。但是document.write()将直接写入现有文档,替换其中的内容,因此段落将消失

由于您使用的是jQuery,因此最好让jQuery为您添加项:

$("body").append("<p>Paragraph count:"+$("p").length+"</p>");
$(“body”).append(段落计数:“++$(“p”).length+”

”);
在主体内创建另一个标记

<div id="plength"></div>

在这里运行示例

在主体内创建另一个标记

<div id="plength"></div>

在此处运行示例

不要使用
文档。编写
。99%是邪恶的。创建一个
div
,并将
par.length
作为其
text()
替换。那么原因和替代方法是什么?
$(“#结果”).text(par.length)
不要使用
文档。请编写
。99%是邪恶的。创建一个
div
,并将
par.length
作为其
text()
替换。那么原因和替代方法是什么?
$(“#结果”).text(par.length)
在主体中