Grails:如何动态添加 css 样式到页面?
Grails: How to add css styles to page dynamicly?
我写的插件,有这样的标签库:
class MyTagLib {
static namespace = "my"
Closure someWidget = { attrs ->
// Need somehow add styles to page
// ...
}
}
我使用 asset-pipeline
插件,但找不到任何方法来动态添加样式表(到 head
标记中)。
应用程序的基本 gsp 布局(不是我的插件):
<!DOCTYPE html>
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<g:layoutHead/>
<title><g:layoutTitle/></title>
</head>
<body>
<g:layoutBody/>
<asset:deferredScripts/>
</body>
</html>
我在 taglib 中需要类似的东西:
g.putItIntoHead(asset.stylesheet(src: 'assets/my.css'))
您正在寻找的是一个可以注入 javascript 代码的标签。
class MyTagLib {
static namespace = "my"
Closure putItIntoHead= { attrs ->
out << " var head = document.head ";
out << " var link = document.createElement('link')";
out << " link.type = 'text/css'";
out << " link.rel = 'stylesheet'";
out << " link.href = '{url}'";
out << " head.appendChild(link)";
}
}
您也可以使用 jquery,(查看 this 问题了解更多)
而且您不一定要将它添加到头部。
我写的插件,有这样的标签库:
class MyTagLib {
static namespace = "my"
Closure someWidget = { attrs ->
// Need somehow add styles to page
// ...
}
}
我使用 asset-pipeline
插件,但找不到任何方法来动态添加样式表(到 head
标记中)。
应用程序的基本 gsp 布局(不是我的插件):
<!DOCTYPE html>
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<g:layoutHead/>
<title><g:layoutTitle/></title>
</head>
<body>
<g:layoutBody/>
<asset:deferredScripts/>
</body>
</html>
我在 taglib 中需要类似的东西:
g.putItIntoHead(asset.stylesheet(src: 'assets/my.css'))
您正在寻找的是一个可以注入 javascript 代码的标签。
class MyTagLib {
static namespace = "my"
Closure putItIntoHead= { attrs ->
out << " var head = document.head ";
out << " var link = document.createElement('link')";
out << " link.type = 'text/css'";
out << " link.rel = 'stylesheet'";
out << " link.href = '{url}'";
out << " head.appendChild(link)";
}
}
您也可以使用 jquery,(查看 this 问题了解更多)
而且您不一定要将它添加到头部。