开发者

dynamically adding a css class to a div/span [duplicate]

This question already has answers here: 开发者_开发问答 Closed 12 years ago.

Possible Duplicate:

Add another class to a div with javascript

How can i dynamically add/remove a class to a span?


Set the property 'className' on the span. This will set its css class


Well, you could use jQuery to manipulate the span.

<span></span>

<script type="text/javascript">
$("span").addClass("addedClass");
$("span").removeClass("addedClass");
</script>


You could use PHP to change the class. Using some sort of link to link between different php instances.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜