javascript 将 css 类动态添加到 div/span
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4955899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
dynamically adding a css class to a div/span
提问by jslearner
Possible Duplicate:
Add another class to a div with javascript
How can i dynamically add/remove a class to a span?
我如何动态地添加/删除一个类到一个跨度?
回答by TurBas
Set the property 'className' on the span. This will set its css class
在跨度上设置属性“className”。这将设置它的 css 类
回答by jimmystormig
Well, you could use jQuery to manipulate the span.
好吧,您可以使用 jQuery 来操作跨度。
<span></span>
<script type="text/javascript">
$("span").addClass("addedClass");
$("span").removeClass("addedClass");
</script>
回答by RichieAHB
You could use PHP to change the class. Using some sort of link to link between different php instances.
您可以使用 PHP 来更改类。使用某种链接在不同的 php 实例之间进行链接。
Here would be the "button":
这是“按钮”:
<div class="info_tab">
<p><?php if($_GET['state'] !='1') echo '<a href="?state=1">click to change class</a>'; else echo '<a href="page.php">click to change class back</p>' ?></p>
</div>
Here would be the changing span:
这是变化的跨度:
<span <?php if(isset($_GET['state']) && $_GET['tab'] == 1) echo 'class="state1"'; else echo 'class="state2"';?>>More information</span>
It's nice to have a non-javascript way for compatibility reasons for some users.
出于兼容性原因,对于某些用户来说,使用非 JavaScript 的方式很好。

