简体   繁体   English

使用javascript动态添加sylesheet到head

[英]dynamically add sylesheet to head using javascript

I need to have the old "resize fonts" option at the top of the site I'm building - one link that is the default size, one that makes fonts a size bigger, and one that makes them two sizes bigger. 我需要在我正在构建的网站顶部使用旧的“调整大小字体”选项 - 一个是默认大小的链接,一个使字体更大,另一个使它们的大小更大。 it only needs to affect some nav and body copy, so I'd like to simply make 2 extra stylesheets to just style those elements, and upon clicking one of the links, load an additional stylesheet into the header. 它只需要影响一些导航和正文复制,所以我想简单地制作2个额外的样式表来设置这些元素的样式,点击其中一个链接后,在标题中加载一个额外的样式表。 when you click the "default" link, it will go back to the original size (with no additional stylesheets loaded). 当您单击“默认”链接时,它将返回到原始大小(没有加载其他样式表)。

Is there a way to do this in javascript? 有没有办法在javascript中执行此操作? This is a Wordpress site. 这是一个Wordpress网站。 Alternately, I could use js to add a tag to the body and target the elements that way. 或者,我可以使用js向主体添加标签并以这种方式定位元素。

What is the best way to do this? 做这个的最好方式是什么?

You should just change the class of the elements. 您应该只更改元素的类。 That will change their css to whatever is specified by that class in the stylesheet. 这会将他们的css更改为样式表中该类指定的任何内容。 Your first option is more complicated than it needs to be. 你的第一个选择比它需要的更复杂。 If you use JQuery you can literally make this one line of code. 如果你使用JQuery,你可以从字面上做出这一行代码。

$("span#applicableId").each(function(){ 
    $(this).class("theOtherClass");
});

You can dynamically add a stylesheet on the fly by using something like below: 您可以使用以下内容动态地动态添加样式表:

function loadNewStyleSheet() {
        var style = document.createElement('style');
        style.type = 'text/css';
        style.src = 'http://path/to/cssfile'
        document.body.appendChild(style);
            }

I would do this as an include statement in in a php tag. 我会在php标签中作为include语句执行此操作。 Then just have one php file you include on the top of each page. 然后只需要在每个页面的顶部包含一个php文件。

php inlude info: http://php.net/manual/en/function.include.php php inlude info: http//php.net/manual/en/function.include.php

then from there I bet there is some code out there you could reuse. 然后从那里我打赌有一些你可以重用的代码。

For example maybe this could be of some help? 例如,这可能会有所帮助吗? it is a php script that will ajust the css styling of the page and not interfer with other parts. 它是一个PHP脚本,它将调整页面的CSS样式,而不会干扰其他部分。 http://www.phpclasses.org/package/1296-PHP-Resizes-text-on-a-page-using-styles-and-sessions-.html http://www.phpclasses.org/package/1296-PHP-Resizes-text-on-a-page-using-styles-and-sessions-.html

I guess that it is not possible to add it dynamically to head tag. 我想不可能将它动态添加到head标签。 Of course, you can do that, but this stylesheet won't be loaded. 当然,您可以这样做,但不会加载此样式表。 Maybe you should try to get it via ajax and append to a special script tag in your site. 也许您应该尝试通过ajax获取它并附加到您站点中的特殊脚本标记。 If user will choose another option, then you will load another content, clear this script tag and append new content there. 如果用户将选择其他选项,则您将加载其他内容,清除此脚本标记并在其中添加新内容。

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

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