如何在我的页面中包含外部html文件?

时间:2022-01-25 09:54:59

I want to do:

我想要做:

<script src='bob.js'></script>

But using a html file, ie.

但是使用html文件,即。

<script type='html' src='bob.html'></script>

The html file contains multiple JQuery templates, so it will have to parse the html file for tags.

html文件包含多个JQuery模板,因此它必须解析html文件中的标签。

The src will be dynamic. I suspect the solution is JQuery, however I want it to be synchronous, just like with the first example.

src将是动态的。我怀疑解决方案是JQuery,但我希望它是同步的,就像第一个例子一样。

How can I achieve this? I don't want to use another plugin, just native JQuery.

我怎样才能做到这一点?我不想使用另一个插件,只是本机JQuery。

1 个解决方案

#1


3  

you can get access to it using ajax:

你可以使用ajax访问它:

function getPageObj(urlPath){
    var response =  $.ajax({
        type: "GET",
        dataType:'html',
        async:false,
        cache:true,
        url: urlPath
    }).responseText;

    if(response==null ||response.length<1){return null;}
    var obj = $(response);

    return obj;
}

#1


3  

you can get access to it using ajax:

你可以使用ajax访问它:

function getPageObj(urlPath){
    var response =  $.ajax({
        type: "GET",
        dataType:'html',
        async:false,
        cache:true,
        url: urlPath
    }).responseText;

    if(response==null ||response.length<1){return null;}
    var obj = $(response);

    return obj;
}