简体   繁体   English

将样式应用于包含jquery的html文件

[英]Apply styles to an jquery included html file

I am trying to perform a jquery load of a html page into the main body of a page. 我正在尝试将html页面的jquery加载到页面的主体中。

Using a div named sidebar_menu that is in the middle of the page, i am performing a jquery load at the end(bottom) of the page. 使用位于页面中间的名为sidebar_menu的div,我正在页面的末尾(底部)执行jquery加载。

    $("#sidebar_menu").load("/sitemenu.html");
    $("#sidebar_menu").page();

This kinda works... the content is displayed, but the menu does not have the javascript functionality (expand, collapse, etc) applied to it. 有点用 ...显示内容,但是菜单未应用javascript功能(展开,折叠等)。 The styles have been applied, but the functionality of the menu is not there. 样式已被应用,但是菜单的功能不存在。

I can copy the contents of the html in place of the div, and the menu operations work. 我可以复制html的内容代替div,并且菜单操作有效。

Am i loading the included file too late in the stack? 我在堆栈中加载包含的文件是否太晚了? currently using the 目前正在使用

jQuery(document).ready(function(){
    $("#sidebar_menu").load("/sitemenu.html");
    $("#sidebar_menu").page();
});

but is there better area to load the html file into the DOM, as the .ready seems to be too late in the page assembly stack to be operational. 但是有更好的区域将html文件加载到DOM中,因为.ready在页面程序集堆栈中似乎为时已晚,无法运行。

thank you 谢谢

There are many JQuery methods that strip Javascript. 有许多剥离Javascript的JQuery方法。 I learned it the hard way. 我很难学。 Look into that. 调查一下。 It may not be the problem you are guessing. 这可能不是您正在猜测的问题。 The way around it is to not get the JS generated on the server side but to have it on the client side with parameter, config, etc. values passed as some DATA- element values from the server side for some HTML elements. 解决方法是不要在服务器端生成JS,而要在客户端将它与参数,配置等值一起作为参数从服务器端传递给某些HTML元素,作为DATA-元素值。 That string that you assign to DATA- can be a JSON string too. 您分配给DATA-的字符串也可以是JSON字符串。

You should use jQuery .on() method see http://api.jquery.com/on/ 您应该使用jQuery .on()方法,请参见http://api.jquery.com/on/

I am not sure how your code looks like. 我不确定您的代码是什么样子。 But here is the idea. 但这是个主意。 Take the closest container (that exists in DOM) of the element that will be loaded (not in DOM at that moment) and on that asign selector and action for elements to be loaded. 取将要加载的元素的最接近的容器(在DOM中存在)(当时不在DOM中),并该符号选择器和要加载的元素上执行操作。

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

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