使用YUI3动态加载样式表

发布于 2024-08-22 02:47:36 字数 248 浏览 3 评论 0原文

我使用 YUI3 'Get' 动态加载样式表。然而,据我所知,它没有给我任何方法来为这些新样式表设置 id,这会导致样式表在用户浏览网站时多次加载。

var css_obj = Y.Get.css(location.pathname+"/../css/"+jta["iss"]+".css");

有没有人有任何其他方法可以做到这一点,以便我可以在加载 css 之前进行测试(如果它已经加载)?

谢谢

I am dynamically loading stylesheets using YUI3 'Get'. which is working flawlessly however, as far as I can tell it doesn't give me any way to set id's for these new stylesheets which is causing the stylesheet to be loaded multiple times as the user navigates around the site.

var css_obj = Y.Get.css(location.pathname+"/../css/"+jta["iss"]+".css");

Does anyone have any other way of doing this so that I can test prior to loading the css, if it has already been loaded?

Thanks

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

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

发布评论

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

评论(2

坠似风落 2024-08-29 02:47:36

您是否尝试使用自己的变量来检查它是否已被加载?

var css_obj;

function LoadCss() {
    if (css_obj == null) {
        css_obj = Y.Get.css(location.pathname+"/../css/"+jta["iss"]+".css");
    }
}

如果你使用 jQuery 你可以写

$(document).ready(function(){ LoadCss(); });

Did you try using your own variable to check if it's already been loaded?

var css_obj;

function LoadCss() {
    if (css_obj == null) {
        css_obj = Y.Get.css(location.pathname+"/../css/"+jta["iss"]+".css");
    }
}

and if you're using jQuery you could write

$(document).ready(function(){ LoadCss(); });
甜点 2024-08-29 02:47:36

我喜欢亨特的解决方案。不过,您确实可以访问插入的 link 节点,并且您可以根据需要设置其 id:

http://ericmiraglia.com/yui/demos/cssid.php

YUI().use("get", function(Y) {

    Y.Get.css("http://ericmiraglia.com/yui/demos/red.css", {
        onSuccess: function(o) {
        o.nodes[0].setAttribute("id", "myElementId");
        alert(o.nodes[0].getAttribute("id"));
    }
});
});

I like Hunter's solution. You do have access to the inserted link node, though, and you can set its id if you wish:

http://ericmiraglia.com/yui/demos/cssid.php

YUI().use("get", function(Y) {

    Y.Get.css("http://ericmiraglia.com/yui/demos/red.css", {
        onSuccess: function(o) {
        o.nodes[0].setAttribute("id", "myElementId");
        alert(o.nodes[0].getAttribute("id"));
    }
});
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文