How can I embed a webpage within another and isolate CSS
I need to embed one webpage within another, the inner page will be wrapped by a <div>
and not contain the <html>
, <head><title>
or stuff like that, however, the inner page can contain <link>
's to CSS that I don't want to affect the outer page
I currently fetch the HTML with AJAX and insert it into the outer DOM, to workaround the styles conflicting I extract any links prior to embedding into the DOM, fetc开发者_如何学Ch that CSS with AJAX, parse the styles and apply them inline using jQuery selectors.
That has obvious problems with things like pseudo-selectors, however, the main problem is that styles from the outer page affect the inner page, I cant reasonably reset every possible style, and I need to access the inner pages dom so using an iframe is out of the question.
Its a fairly complex setup, but I was wondering if anyone had seen anything along similar lines or had a nicer approach.
Cheers Dale
You could assign a unique id to the div and prepend the selector to all the rules in the css.
HTML Before
<div>
<!--start ajax content -->
<a href="#"> Content </a>
<!--end ajax content -->
</div>
CSS Before
a {color:#999;}
HTML After
<div id="unique0001">
<!--start ajax content -->
<a href="#"> Content </a>
<!--end ajax content -->
</div>
CSS After
#unique0001 a {color:#999;}
精彩评论