简体   繁体   English

是否可以在包含的 HTML 文件中的元素上使用 jQuery?

[英]Is it possible to use jQuery on an element that is in an included HTML file?

As title sais, is it possible to use jQuery on an element that is in an included HTML file?正如标题所说,是否可以在包含的 HTML 文件中的元素上使用 jQuery? either with HTML or jQuery include.使用 HTML 或 jQuery 包含。

Extra info:额外信息:

Example: I have A.html and B.html which both includes 1.html示例:我有 A.html 和 B.html,它们都包含 1.html

There is an element in 1.html which I dont want to show in A.html but I do want it to show in B.html. 1.html 中有一个元素,我不想在 A.html 中显示,但我确实希望它在 B.html 中显示。

Thanks in advance.提前致谢。

If you load some additional DOMs from external resources, make sure you initiate function after all DOMs loaded, or use global selectors.如果您从外部资源加载一些额外的 DOM,请确保在所有 DOM 加载后启动函数,或者使用全局选择器。

Let's suppose you have <div class="foo"></div> loaded after main document finished, to set onclick listener of that element from main document use:假设您在主文档完成后加载了<div class="foo"></div> ,以从主文档使用设置该元素的onclick侦听器:

$(document).on('click', '.foo', function() { ... });

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

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