How do you use javascript variables when writing inline CSS styles in a Jade template
题
I'm trying to dynamically insert snippets of CSS. Ideally, this would work:
style(type='text/css')
#header a#logo { background:url(constants.logo) no-repeat; }
@media only screen and (-webkit-min-device-pixel-ratio: 1.5) {
#header a#logo { background-image: url(constants.logo_2x); }
}
Unfortunately, constants.logo is /literally/ placed in the DOM. What does work is the following:
!= "<style type='text/css'>"
!= "#header a#logo { background:url('"+constants.logo+"') no-repeat; }"
!= "@media only screen and (-webkit-min-device-pixel-ratio: 1.5) {"
!= " #header a#logo { background-image: url('"+constants.logo_2x+"'); }"
!= "}</style>"
Is there a better way?
解决方案
Use #{variable}
to print a variable within an element's text.
不隶属于 StackOverflow