简体   繁体   English

如何使特定按钮保持活动状态? CSS,Javascript

[英]How to make specific button to stay active ? CSS, Javascript

having a problem, I have a javascript content switcher on a page, but I can't seem to get one thing working - how to make a clicked button stay active after it's clicked? 有问题,我在页面上有一个javascript内容切换器,但我似乎无法让一件事工作 - 如何让点击按钮在点击后保持活动状态?

Here's a code: JS 这是一个代码: JS

<script type="text/javascript">
function switch1(div) {
var option=['one','two','three'];
for(var i=0; i<option.length; i++) {
if (document.getElementById(option[i])) {
obj=document.getElementById(option[i]);
obj.style.display=(option[i]==div)? "block" : "none";
}
}
}

window.onload=function () {switch1('one');}
</script>

CSS CSS

#switchables li a {
    color: #262626;
    text-decoration: none;
    display: inline-block;
    padding-top: 14px;
    padding-right: 34px;
    padding-bottom: 10px;
    padding-left: 33px;
    background-image: url(img/catButBcgr.jpg);
    border-right-width: 1px;
    border-left-width: 1px;
    border-right-style: solid;
    border-left-style: none;
    border-right-color: #E1E1E1;
    border-left-color: #FFF;
}
#switchables li a:hover {
    background-image: url(img/catButBcgrH.jpg);
}
#switchables li a:active {
    background-image: url(img/catButBcgrA.jpg);
}

HTML HTML

 <ul id="switchables">
   <li><a class="active" href="javascript:void[0];" onclick="switch1('one');">OVERVIEW</a></li>
   <li><a class="active" href="javascript:void[0];" onclick="switch1('two');">CATEGORY</a></li>
   <li><a class="active" href="javascript:void[0];" onclick="switch1('three');">CATEGORY</a></li>
</ul>

You need to make an "Active" class and add it to the button when clicked. 您需要创建一个“活动”类,并在单击时将其添加到按钮。

#switchables a:active, #switchables a.active {
    background-image: url(img/catButBcgrA.jpg);
}

It's easy using jQuery: 使用jQuery很容易:

$(document).ready(function() {
    myInit()
})

function myInit() {
    $('#switchables a').click(function() {
        $('#switchables a').removeClass('active')
        $(this).addClass('active')
    })
}

This is a nice opportunity to learn. 这是一个很好的学习机会。 Diodeus' answer is completely right, but his jQuery code does horrible things on the background, see the comments: Diodeus的答案是完全正确的,但他的jQuery代码在后台做了可怕的事情,请参阅评论:

$(document).ready(function() {
    myInit()
})

function myInit() {
  // on the following line, jQuery creates an array of objects (a tags)
  // (costly operation!) and adds click listener to each of them
  $('#switchables a').click(function() {
    // on the following line, jQuery creates the crazy God object again
    // and throws it off after this command
    // for each a tag and tries to remove class active from it
    // in only one case it actually does something - mere class removal
    // btw removeClass is ridiculous function if you dig into jQuery 1.10 source  
    $('#switchables a').removeClass('active')
    // this = the source of click event, not jQuery object
    $(this).addClass('active')
  })
}

This is just a very short code, now imagine you write whole web this style. 这只是一个非常短的代码,现在想象你用这种风格写整个网络。 It will be observably slower, consuming much more resources. 它会明显变慢,消耗更多的资源。

If you insist on jQuery, try to write reusable code a little: 如果您坚持使用jQuery,请尝试稍微编写可重用的代码:

function myInit() {
  // jQuery object is created only once
  var $anchors = $('#switchables a');
  $anchors.click(function() {
    // ...and reused here
    $anchors.removeClass('active')
    $(this).addClass('active')
  });
}

But you'd do much better job using native javascript 但是你使用原生javascript做得更好

var items = document.querySelectorAll("#switchables a");
var prev = items[0];
[].forEach.call(items,function(item) {
  item.addEventListener("click",function() {
    // no need to loop every a tag here
    prev.classList.remove("active");
    item.classList.add("active");
    // remember previous active a tag
    prev = item;
  });
});

document.querySelectorAll is a live collection which is something that can't be achieved by any javascript library, it is implemented in underlying and more effective code of the browser. document.querySelectorAll是一个实时集合 ,它是任何javascript库都无法实现的,它是在底层和更有效的浏览器代码中实现的。

Advice Don't use jQuery until you know Javascript well. 建议在你熟悉Javascript之前不要使用jQuery。 Without that knowledge, you will be able to implement just basic animations, copy&paste some plugins and nothing more. 没有这些知识,您将能够实现基本的动画,复制和粘贴一些插件,仅此而已。 And when you know Javascript on some level, you will probably see very little reason to use jQuery anymore. 当你在某种程度上了解Javascript时,你可能会发现使用jQuery的理由很少。

In the code above, jQuery can be easily removed: 在上面的代码中,可以轻松删除jQuery:

1: $(document).ready(handler) -> document.addEventListener("readystatechange",handler); 1: $(document).ready(handler) - > document.addEventListener("readystatechange",handler);

2: $('#switchables a') -> document.querySelectorAll("#switchables a"); 2: $('#switchables a') - > document.querySelectorAll("#switchables a");

3: $(nodeList).click(handler) -> 3: $(nodeList).click(handler) - >

[].forEach.call(nodeList,function(node) {
  // you can reuse node here, unlike the jQuery
  node.addEventListener("click",handler);
});

4: $(node).removeClass(className) -> node.classList.remove(className) 4: $(node).removeClass(className) - > node.classList.remove(className)

5: $(node).addClass(className) -> node.classList.add(className) 5: $(node).addClass(className) - > node.classList.add(className)

It is a few chars longer. 这是几个字符更长。 But it is more reusable, readable, effective and it is not God object or Cargo cult programming. 但它更具有可重用性,可读性和有效性,而且不是上帝对象或Cargo cult编程。

The native codes above are javascript standards and are supported in any decent browser. 上面的本机代码是javascript标准,并且在任何体面的浏览器中都受支持。 Three years ago, when Diodeus provided his answer, IE8 was an issue here. 三年前,当Diodeus提供他的答案时,IE8是一个问题。 But it is dead now (under 2% worldwide according to gs.statcounter). 但它现在已经死了(根据gs.statcounter,全球不到2%)。 Help it die completely by not supporting it. 通过不支持它来帮助它完全死亡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM