简体   繁体   English

jQuery ajax或xmlhttp的javascript加载问题

[英]javascript loading issue with jquery ajax or xmlhttp

Hi We using a master template for all aspx pages. 您好,我们为所有aspx页面使用主模板。 In master page we added frequent using CSS, JS, In aspx page pages we using require CSS and JS for that particular page. 在母版页中,我们添加了频繁使用CSS,JS的内容。在aspx页中,我们使用了特定页面的CSS和JS。

Now we are trying add Jquery Ajax or XmlHttp for website optimization. 现在,我们尝试添加Jquery Ajax或XmlHttp来优化网站。 Using Jquery ajax we calling a aspx page into DIV. 使用Jquery ajax,我们将aspx页面调用到DIV中。 Here problem when we load total aspx page from HTML tags javascript and all scripts working fine. 当我们从HTML标记javascript加载全部aspx页面并且所有脚本运行正常时,这里出现问题。 But when we trying to load that page from particular DIV or body then scripts not working. 但是,当我们尝试从特定的DIV或正文加载该页面时,脚本将无法正常工作。

Can please tell me how we can load scripts when loading page from particular DIV or body. 请告诉我从特定DIV或正文加载页面时如何加载脚本。

In your aspx page make sure you do not have relative paths for your CSS and JS files. 在aspx页面中,确保没有CSS和JS文件的相对路径。 The following is using a relative path. 以下是使用相对路径。 If you load the file through an ajax request and into a div the relative paths will not work. 如果通过ajax请求将文件加载到div中,则相对路径将不起作用。

    <link rel="stylesheet" type="text/css" href="../Common/css/myCss.css" media="all" />
    <script type="text/javascript" src="../Common/js/myJs.js"></script>

Instead you must use the absolute path of the file 相反,您必须使用文件的绝对路径

    <link rel="stylesheet" type="text/css" href="<%=Page.ResolveUrl("../Common/css/myCss.css") %>" media="all" />
    <script type="text/javascript" src="<%=Page.ResolveUrl("../Common/js/myJs.js") %>"></script>

Try this and see if it works. 试试这个,看看是否可行。 I have not tested it in the way you want to use it. 我没有按照您想要使用它的方式进行测试。

Edit: 编辑:

If you are receiving the page as an html string, instead of doing this: 如果您以html字符串的形式接收页面,请执行以下操作:

    $(result).find("#ContentSwitcher").html()

try the following: 尝试以下方法:

    $.parseHTML(result).find("#ContentSwitcher").html()

Note: $.parseHTML requires jquery 1.8.0 or higher. 注意:$ .parseHTML需要jquery 1.8.0或更高版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM