jQuery追加不起作用(将css添加到head)

jQuery append not working(adding css to head)

本文关键字:添加 head css 追加 不起作用 jQuery      更新时间:2023-09-26

我想在页首添加样式表,但由于某些原因,它不起作用。我应该使用其他东西而不是追加吗?

代码

$('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);

您可以进行

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

您应该将代码放入文档中,以便加载DOM。你是吗?然后类似于:

$(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"
    });
});

示例位于:http://jsfiddle.net/XjAu4/