如何从外部html替换div的内容?

时间:2021-11-17 21:11:54

I want catch a content of a div. That div there is in a foreign html. I tried

我想抓住div的内容。那个div有一个外国的html。我试过了

$(document).ready(function(){
               $('#mainbox').load('includes/divone.html');
           });

div mainbox is a maincontent

div mainbox是一个主要内容

The html page divone.html is :

html页面divone.html是:

<body>
<div id="div1">hey bulldog!</div>


</body>

But it doesn't run!

但它没有运行!

1 个解决方案

#1


Are you waiting for the page to be loaded? Remember that load is a non-blocking api.

你在等待页面加载吗?请记住,load是一个非阻塞的api。

Here is the code that works for me

这是适合我的代码

Main Page:

<!doctype html>
<html>
<head>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div id="mainbox"></div>
<script>
$(document).ready(function(){
        $( "#mainbox" ).load( "/load.html", function () {
             $('#div1').html("hello world");
        });
});
</script>
</body>
</html>

Loaded Page:

<body>
<div id="div1"></div>
</body>

#1


Are you waiting for the page to be loaded? Remember that load is a non-blocking api.

你在等待页面加载吗?请记住,load是一个非阻塞的api。

Here is the code that works for me

这是适合我的代码

Main Page:

<!doctype html>
<html>
<head>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div id="mainbox"></div>
<script>
$(document).ready(function(){
        $( "#mainbox" ).load( "/load.html", function () {
             $('#div1').html("hello world");
        });
});
</script>
</body>
</html>

Loaded Page:

<body>
<div id="div1"></div>
</body>