javascript函数中的Css代码 [英] Css code in javascript function
本文介绍了javascript函数中的Css代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个javascript方法:
I have this javascript method:
<script type="text/javascript">
function MyFunction(sender, eventArgs) {
if (someCondition) {
//css
}
}
</script>
我想要执行的css代码是:
The css code I want to be executed is:
<style type="text/css">
.classInsideTheClassWhichEntersTheIf
{
background: url(Images/myImage.png) !important;
}
</style>
但仅限于那些输入上述if条件的单元格。如果我在外面写它,它适用于每个细胞。这样的事情可能吗?如果是,该怎么做?
but only for those cells that enter the if condition above. If I write it outside it works but for every cell. Is something like this possible? If yes, how to do it?
推荐答案
有几种方法可以做到这一点。
There are several ways to do this.
选项1。
<script type="text/javascript">
function MyFunction(sender, eventArgs) {
if (someCondition) {
someelement.style.cssText = "background: url(Images/myImage.png) !important;"
}
}
</script>
选项2。
<script type="text/javascript">
function MyFunction(sender, eventArgs) {
if (someCondition) {
someelement.className = "someclass"
}
}
</script>
其中,
<style>
.someclass{
background: url(Images/myImage.png) !important;
}
</style>
选项3
<script type="text/javascript">
function MyFunction(sender, eventArgs) {
if (someCondition) {
someelement.setAttribute('style', 'background: url(Images/myImage.png) !important;');
}
}
</script>
这是伪代码,
if(condition)
someelement.style.cssText = "background: url(Images/myImage.png) !important;";
这篇关于javascript函数中的Css代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文