我有这个javascript方法:
<script type="text/javascript">
function MyFunction(sender, eventArgs) {
if (someCondition) {
//css
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
我想要执行的css代码是:
<style type="text/css">
.classInsideTheClassWhichEntersTheIf
{
background: url(Images/myImage.png) !important;
}
</style>
Run Code Online (Sandbox Code Playgroud)
但仅限于那些进入上述if条件的单元格.如果我在外面写它,它适用于每个细胞.这样的事情可能吗?如果是的话,该怎么办?
Ash*_*thy 14
有几种方法可以做到这一点.
选项1.
<script type="text/javascript">
function MyFunction(sender, eventArgs) {
if (someCondition) {
someelement.style.cssText = "background: url(Images/myImage.png) !important;"
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
选项2.
<script type="text/javascript">
function MyFunction(sender, eventArgs) {
if (someCondition) {
someelement.className = "someclass"
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
哪里,
<style>
.someclass{
background: url(Images/myImage.png) !important;
}
</style>
Run Code Online (Sandbox Code Playgroud)
选项3
<script type="text/javascript">
function MyFunction(sender, eventArgs) {
if (someCondition) {
someelement.setAttribute('style', 'background: url(Images/myImage.png) !important;');
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
这是一个伪代码,
if(condition)
someelement.style.cssText = "background: url(Images/myImage.png) !important;";
Run Code Online (Sandbox Code Playgroud)
<script type="text/javascript">
function MyFunction(sender, eventArgs) {
if (someCondition) {
someelement.style.backgroundImage = "url(Images/myImage.png)";
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
!important这里是不必要的,因为内联样式会覆盖非内联样式。
| 归档时间: |
|
| 查看次数: |
39065 次 |
| 最近记录: |