简体   繁体   English

在不使用iframe的情况下隔离HTML元素

[英]Isolating HTML element without using iframe

I'm building a MVC application with angular as client-side framework. 我正在构建一个以angular为客户端框架的MVC应用程序。 I'm loading all views inside tabs; 我正在加载选项卡中的所有视图; the user can open the same view multiple times and here comes the problem. 用户可以多次打开同一个视图,这就出现了问题。

when the user opens the same view twice, there will be duplicated IDs hence some html attributes won't work correctly (eg "for" attribute for "label" element) 当用户两次打开同一视图时,将有重复的ID,因此某些html属性将无法正常工作(例如,“ label”元素的“ for”属性)

I know, I can generate random IDs to avoid this problem but this's not acceptable in my case. 我知道,我可以生成随机ID来避免此问题,但这在我的情况下是不可接受的。

what i need is to isolate the view root (div element) so it doesn't interfere with other elements, something like iframe but without using iframe :) 我需要隔离视图根(div元素),以便它不会干扰其他元素,例如iframe但不使用iframe :)

You cannot. 你不能。 The DOM structure resides inside the window and the document which follows. DOM结构位于window和随后的document中。 There can be only one window pr (i)frame. 只能有一个window pr(i)帧。

What you could do, and the most common approach, is to generate UUID for the specific tab/view. 您可以做的,也是最常用的方法,是为特定的选项卡/视图生成UUID。 This UUID must be available in your backend and is how you direct form posts to the correct context. 此UUID必须在您的后端中可用,这是将表单发布定向到正确上下文的方式。

Now, while youre generating the HTML for each tab, postfix your ID's with the UUID. 现在,当您为每个选项卡生成HTML时,请使用UUID在ID后缀。 Keep in mind though, that if your main view is 100% static without page reloads, the DOM will keep stacking up - and eventually take up a great deal of memory. 但是请记住,如果您的主视图是100%静态的而没有页面重新加载,则DOM将不断堆积-并最终占用大量内存。

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

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