创建<样式>JavaScript 中的标签,然后访问 cssRules

发布于 2024-10-15 23:40:06 字数 620 浏览 6 评论 0原文

我真的希望这不是一个愚蠢的问题......在过去的几个小时里我一直在用头撞墙试图解决它。

这本质上是 How to create a

谁能解释一下如何为 iframe 创建样式表,以便在 Chrome 和 Safari 中为我提供“cssRules”属性?

我的目的是将样式表添加到我在网页中创建的 iframe 中,因为它似乎没有附带样式表。后来,我希望将一些样式从封闭文档的样式表复制到 iframe 的样式表,但我还没有做到这一点。一旦我解决了上述问题,如果有人知道这样做的任何警告,我会提前感激不尽。

I really hope this isn't a stupid question...I've been beating my head against the wall for the last few hours trying to solve it.

This is essentially a followup question to How to create a <style> tag with Javascript, elsewhere on Stack Overflow. The techniques described by Tom and Michael work great in IE, Firefox, and Opera, and almost work in Chrome and Safari. The problem is, in Chrome and Safari, no "cssRules" property is created for the style sheet! The other browsers create a "cssRules" property (well, IE creates a "rules" property, but you knew that already).

Can anyone explain how to create a style sheet for an iframe that'll give me a "cssRules" property in Chrome and Safari?

My intention is to add a style sheet to an iframe that I created in my web page, since it doesn't seem to come with one. Later, I hope to copy some of the styles from the enclosing document's style sheet to the iframe's style sheet, but I haven't gotten there yet. If anyone knows of any caveats to doing that once I've solved the above problem, I'd be grateful in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走过海棠暮 2024-10-22 23:40:06

我已经在 Firefox 3.6、Chromium 8、Opera 11 (ubuntu 10.10)

temp.html 中进行了测试:

<style>
    body { background-color: blue; }
</style>

 <body>
    Hello
</body>

index.html:

<html>
<head>
    <script>
        function test(){
            // your new <style> tag
            var stl = document.createElement("style");
            // its content
            stl.innerHTML = "body { background-color: red}";

            // getting iframe
            var myIframe = document.getElementById("myiframe");

            // apply new CSS to iframe
            myIframe.contentWindow.document.
                    getElementsByTagName('head')[0].appendChild(stl);
        }
    </script>
</head>
<body>
    <iframe id="myiframe" src="temp.html"></iframe>
    <button onclick="test()">
</body>
</html>

当我单击按钮时,iframe 中的背景变为红色。

PS如果重复的话抱歉。我现在才打开您帖子中指向另一个主题的链接

I've tested in Firefox 3.6, Chromium 8, Opera 11 (ubuntu 10.10)

temp.html:

<style>
    body { background-color: blue; }
</style>

 <body>
    Hello
</body>

index.html:

<html>
<head>
    <script>
        function test(){
            // your new <style> tag
            var stl = document.createElement("style");
            // its content
            stl.innerHTML = "body { background-color: red}";

            // getting iframe
            var myIframe = document.getElementById("myiframe");

            // apply new CSS to iframe
            myIframe.contentWindow.document.
                    getElementsByTagName('head')[0].appendChild(stl);
        }
    </script>
</head>
<body>
    <iframe id="myiframe" src="temp.html"></iframe>
    <button onclick="test()">
</body>
</html>

When I click the button, background in iframe becomes red.

P.S. Sorry if it is repeating. I've only now opened the link in your post to another topic

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文