使用ajax加载的内容加载脚本和css

时间:2013-05-02 10:52:26

标签: jquery css ajax

我有一个页面,其中包含一个带有链接的菜单边栏和一个div,从其中的其他页面加载ajax内容,内容加载成功但脚本和css不起作用,我也已经使用了html 5历史加载内容来自其他页面。

$(function () {

    $('.menuAnchor').click(function (e) {
        href = $(this).attr("href");
        loadContent(href);
        // HISTORY.PUSHSTATE
        history.pushState('', 'New URL: ' + href, href);
        e.preventDefault();
    });

    // THIS EVENT MAKES SURE THAT THE BACK/FORWARD BUTTONS WORK AS WELL
    window.onpopstate = function (event) {
        console.log("pathname: " + location.pathname);
        loadContent(location.pathname);
    };
});

function loadContent(url) {
    // USES JQUERY TO LOAD THE CONTENT
    $.get(url, {}, function (data) {
        $(".contn_btm_mid_bg").html($(data).find('.contn_btm_mid_bg').html());
        //$.validator.unobtrusive.parse(".contn_btm_mid_bg");
    });

    // THESE TWO LINES JUST MAKE SURE THAT THE NAV BAR REFLECTS THE CURRENT URL
    $('li').removeClass('current');
    $('a[href="' + url + '"]').parent().addClass('current');
}

1 个答案:

答案 0 :(得分:0)

确定有一件事使用代码

从外部加载你的css和js
$("<link/>", {
   rel: "stylesheet",
   type: "text/css",
   href: "/styles/yourcss.css"
}).appendTo("head");

与js相同