开发者

jQuery append not working(adding css to head)

I want to add a stylesheet to the head of a page, but for some reason it doesn't work. Should开发者_开发问答 i use something else, instead of append?

the code

$('head').append('<link rel="stylesheet" href="test.css" type="text/css" class="test" />');

maybe some usefull info, i am using firefox 3.6.17


You could try something like this:

loadcss = document.createElement('link');
loadcss.setAttribute("rel", "stylesheet");
loadcss.setAttribute("type", "text/css");
loadcss.setAttribute("href", "test.css");
document.getElementsByTagName("head")[0].appendChild(loadcss);


You could do

jQuery(document.head).append('<link rel="stylesheet" href="test.css" type="text/css" class="test" />');


You should put that code in the document ready, in order to have the DOM loaded. Are you? And then something like:

$(document).ready( function() {
  $("head").append("<link>");
  css = $("head").children(":last");
  css.attr({
    rel:  "stylesheet",
    type: "text/css",
    href: "test.css"
  });
});


try:

$(document).ready( function() {

    $("head").append("<link>");
    var css = $("head").children(":last");
    css.attr({
      rel:  "stylesheet",
      type: "text/css",
      href: "test.css"
    });

});

example is on: http://jsfiddle.net/XjAu4/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜