使用JavaScript

时间:2016-07-18 21:19:04

标签: javascript html css

如何使用JavaScript将第2页的div加载到第1页。

Page2.html

<html>
<head>
<title> title </title>
<body>
<div id="main">
<div id="content2"> this is content2</div>
<div id="content3"> this is content3</div>
</div>
</body>
</html>

我想获取并使用page2中的id content2,在点击并删除链接后,使用该div的内容创建div到page1,并对content3,content4和相继执行相同的操作。

Page1.html

<html>
<head>
<title> title </title>
<body>
<div id="main">
<div id="content1"> this is content1</div>
<a href="#"> get content</a>
</div>
</body>
</html>

然后就是那样。

<html>
<head>
<title> title </title>
<body>
<div id="main">
<div id="content1"> this is content1</div>
<div>this is content2</div>
<div>this is content3</div>
</div>
</body>
</html>

我是JavaScript新手,我没有想法如何做到这一点。如果有人可以帮忙。感谢。

编辑:我想要一种方法只使用javascript和没有jquery这样做,如果这真的可能。我希望我的项目脱机工作,我不能用jquery做,因为它不起作用。我已经下载了jquery插件并将其粘贴到我的目录中,但是,也没有用。

2 个答案:

答案 0 :(得分:1)

您可以结合使用JavaScript,jQuery和AJAX来实现这一目标。

首先,包含jQuery库:

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

然后编写一个与此类似的JavaScript函数,它将使用Page2.html文件替换div的html内容:

var loadNewContent = function {
  $.ajax("Page2.html",{
    success: function(response) {

        $("#content2").html(response);


    }
  }); };

然后你需要一些触发器&#39;运行这个函数,如:

$("#content2").on('click",loadNewContent);

希望这有帮助。

答案 1 :(得分:0)

我使用javascript&amp; amp;编写了一个名为ViaJS的小型库。 jQuery的。它基本上允许您从源到页面加载内容(如div)。看看吧。

Via is a small library that allows you to load content on to a page dynamically

相关问题