点击更改Div样式 [英] Change Div style onclick

查看:133
本文介绍了点击更改Div样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网页顶部有2个标签。当一个选项卡被点击时,我希望该选项卡具有活动类,另一个选项卡具有非活动类,以便用户可以看到当前选择的选项卡。我如何使用javascript / css这样做?

I have 2 tabs at the top of a page. When one tab is clicked, I would like that tab to have an "active" class and the other tab to have an "inactive" class so that the user can see what tab is currently selected. How can I go about doing this with javascript/css?

<div class="tabActive">
Option 1
</div>

<div id="tabInactive">
Option 2
</div>


推荐答案

另一个非jQuery解决方案可能是下面的有两个以上 div

another non-jQuery solution could be the following that works with more than two div:

function changeClass(elClass) {
  var divsLenght = document.getElementsByTagName("div").length;
  for (var i = 0; i < divsLenght; i++) { 
    document.getElementsByTagName("div")[i].className = "tabInactive"; 
  } 
  elClass.className = "tabActive";   
}

演示 http://jsbin.com/opetec/2

这篇关于点击更改Div样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆