我想添加樣式表到頁面的頭部,但由於某種原因它不起作用。我應該使用別的東西,而不是追加?jQuery附加不工作(添加CSS到頭)
代碼
$('head').append('<link rel="stylesheet" href="test.css" type="text/css" class="test" />');
也許一些有用的信息,我使用的Firefox 3.6.17
我想添加樣式表到頁面的頭部,但由於某種原因它不起作用。我應該使用別的東西,而不是追加?jQuery附加不工作(添加CSS到頭)
代碼
$('head').append('<link rel="stylesheet" href="test.css" type="text/css" class="test" />');
也許一些有用的信息,我使用的Firefox 3.6.17
你可以嘗試這樣的事:
loadcss = document.createElement('link');
loadcss.setAttribute("rel", "stylesheet");
loadcss.setAttribute("type", "text/css");
loadcss.setAttribute("href", "test.css");
document.getElementsByTagName("head")[0].appendChild(loadcss);
This works,so it better to use pure javascript ... Thanks !! – user759235 2011-06-16 19:38:27
你可以做
jQuery(document.head).append('<link rel="stylesheet" href="test.css" type="text/css" class="test" />');
我不相信'document.head'被跨瀏覽器支持。 – lonesomeday 2011-06-16 19:39:10
沒有這個不工作... – user759235 2011-06-16 19:42:14
你應該p請準備好文檔中的代碼,以便加載DOM。你是? 然後是這樣的:
$(document).ready(function() {
$("head").append("<link>");
css = $("head").children(":last");
css.attr({
rel: "stylesheet",
type: "text/css",
href: "test.css"
});
});
這個工作,但我使用我的代碼在一個插件,所以這個不工作,或者我可以使用一個文件準備在一個插件? – user759235 2011-06-16 19:40:39
嘿,我遲到了4分鐘:] – WooDzu 2011-06-16 19:44:11
嘗試:
$(document).ready(function() {
$("head").append("<link>");
var css = $("head").children(":last");
css.attr({
rel: "stylesheet",
type: "text/css",
href: "test.css"
});
});
如果要添加HTML,使用$( '頭')HTML(「<鏈接相對。 =「stylesheet」href =「test.css」type =「text/css」class =「test」/>「); - http://api.jquery.com/html/ – John 2011-06-16 19:31:38
這應該工作 - 你包裝它$(function(){}); ? – kinakuta 2011-06-16 19:33:29
不,使用html將刪除頭部內的所有其他代碼。 – user759235 2011-06-16 19:33:54