Jay*_*Jay 56 javascript css jquery
我正在创建一个popupwindow,我想在下面的popupwindow中添加一个css文件是popupwindow的代码.我有一个javascript创建一个弹出窗口.
<a href="popupwindowcontent.xhtml" title="Print" class="popupwindow">Print1</a>
Run Code Online (Sandbox Code Playgroud)
现在我想在这个弹出窗口中添加一个css文件.我试过类似的东西
$('.popupwindow').append('<link rel="stylesheet" href="css/style2.css" type="text/css" />');
$('head').append('<link rel="stylesheet" href="css/style2.css" type="text/css" />');
Run Code Online (Sandbox Code Playgroud)
但不起作用.
谢谢
Eti*_*uis 151
$('head').append('<link rel="stylesheet" href="style2.css" type="text/css" />');
Run Code Online (Sandbox Code Playgroud)
这应该工作.
Dil*_*mar 14
这是我使用jQuery ajax添加css的方法.希望它可以帮助某人..
$.ajax({
url:"site/test/style.css",
success:function(data){
$("<style></style>").appendTo("head").html(data);
}
})
Run Code Online (Sandbox Code Playgroud)
zuz*_*nen 10
var css_link = $("<link>", {
rel: "stylesheet",
type: "text/css",
href: "yourcustomaddress/bundles/andreistatistics/css/like.css"
});
css_link.appendTo('head');
Run Code Online (Sandbox Code Playgroud)
只是我的几美分……有时最好确保没有任何重复……所以我们在 utils 库中有下一个函数:
jQuery.loadCSS = function(url) {
if (!$('link[href="' + url + '"]').length)
$('head').append('<link rel="stylesheet" type="text/css" href="' + url + '">');
}
Run Code Online (Sandbox Code Playgroud)
如何使用:
$.loadCSS('css/style2.css');
Run Code Online (Sandbox Code Playgroud)