第一种写法:
$("<link>")
.attr({ rel: "stylesheet",
type: "text/css",
href: "site.css"
})
.appendTo("head");
第二种写法:
$("head").append("<link>");
css = $("head").children(":last");
css.attr({
rel: "stylesheet",
type: "text/css",
href: "/Content/Site.css"
});
第三种写法(直接在javascript中使用):
function addCSS() {
var link = document.createElement('link');
link.type = 'text/css';
link.rel = 'stylesheet';
link.href = '/Content/Site.css';
document.getElementsByTagName("head")[0].appendChild(link);
}
本文介绍了三种不同的方法来加载CSS文件到网页中。第一种方法使用jQuery的链式调用来创建并附加样式链接;第二种方法也利用了jQuery,但通过append和children方法实现;最后一种方法则是纯JavaScript的方式,通过创建link元素并将其附加到head中。

2818

被折叠的 条评论
为什么被折叠?



