包括另一个html文件和JS中的html文件

时间:2014-02-17 13:13:21

标签: javascript jquery html

假设我有两个html文件,one.html和two.html。我想在one.html中包含two.html,所以我已经这样做了,

<script>
        $(function(){
            $("#middle").load("two.html");
        });
</script>

我有一个脚本文件说,script.js我将在one.html的末尾加载

<script src="scripts/script.js" ></script>

在该文件中,我正在编写要在two.html元素上实现的函数。但这些功能并未应用于two.html。那么,这样做的正确方法是什么?

4 个答案:

答案 0 :(得分:2)

使用$.get jQuery函数

$.get( "tow.html", function( data ) {
  $( "#middle" ).html( data );
  alert( "Load was performed." );
});

答案 1 :(得分:1)

为什么不使用(隐藏?) iframe ?如果它位于同一服务器上,您可以访问父页面(one.html)的功能和元素:

window.parent.example_function();

或使用其DOM元素。

答案 2 :(得分:0)

要在动态加载的元素上添加事件,您需要在 jquery&lt;中使用.live()函数。 1.9 .on()如果 jquery&gt; = 1.9

.live()

.on()

答案 3 :(得分:0)

您可以先加载您的第二页内容,然后仅包含javascript 包含其他HTML。

$.get("two.html", function(data) {
    $('#middle').html(data);
    $('head').append($('<script>').attr('type', 'text/javascript').attr('src', 'scripts/script.js'));
});
相关问题