简体   繁体   English

单击后更改链接的背景颜色

[英]Changing the background color of a link after being clicked

I'd like to ask how to change the background color of each link (the rectangular shape surrounding it) to one different color after a link is clicked, and the other links still remain in its original background color . 我想问一下单击链接后如何将每个链接(围绕它的矩形)的背景色更改为一种不同的颜色,而其他链接仍保持其原始背景色

Each link corresponds to one div placed in the same html file (that I didn't include here). 每个链接对应于放置在同一html文件中的一个div(我在此处未包括)。

The point is to let the viewers know which link they are at. 关键是让观众知道他们在哪个链接上。 By the way, if it is okay I'm looking for the fastest code possible ^_^ (pure css , javascript or jQuery ). 顺便说一句,如果可以的话,我正在寻找最快的代码^ _ ^ (纯cssjavascriptjQuery )。 Appreciate all suggestions! 感谢所有建议!

the highlight only applied to the current link only! 突出显示仅适用于当前链接! (the others will have the normal colors) (其他将具有正常的颜色)

<div id="Navigation">
<div id="nav_link">
  <ul id="MenuBar" class="MenuBarHorizontal">
    <li><a class="MenuBarItemSubmenu" href="javascript:showonlyone('Index_frame');" >Home</a>
      <ul>
        <li><a href="javascript:showonlyone('Specification_frame');" >Specification</a></li>
        <li><a href="javascript:showonlyone('Images_frame');" >Images</a></li>
        <li><a href="javascript:showonlyone('Video_frame');">Video</a></li>
        <li><a href="javascript:showonlyone('Contact_frame');">Contact</a></li>
       </ul>
    </li>
    <li><a href="javascript:showonlyone('Specification_frame');" >Specification</a></li>
    <li><a href="javascript:showonlyone('Images_frame');" >Images</a></li>
    <li><a href="javascript:showonlyone('Video_frame');">Video</a></li>
    <li><a href="javascript:showonlyone('Contact_frame');">Contact</a></li>
    </ul>
</div>
  <!--End of nav_link-->
</div>
<!-- End of Navigation-->

function showonlyone(thechosenone) {
     $('.newboxes').each(function(index) {
          if ($(this).attr("id") == thechosenone) {
               $(this).show(1000).fadeIn(500);
          }
          else {
               $(this).hide(1500).fadeOut(500);
          }
     });
}

EDITED 已编辑

Guys, there is this one thing that I'm still stuck at even though I spent time on it a lot, I added some more JavaScript links the same with the above in the page with the idea that these new links will be functional just like the former. 伙计们,尽管我花了很多时间,但我仍然坚持这一件事,我在页面中添加了与上面相同的更多JavaScript链接,以为这些新链接将像前者。 That is being clicked on ==> the highlight will appear only on these Navigation links. 在==>上单击,突出显示将仅出现在这些导航链接上。 I tried to modify the function from jjurm like this 我试图像这样从jjurm修改功能

$(function(){
    $("#MenuBar a,#colOne a").bind("click", function(){
        var names=$(this).attr('name');
        $("#MenuBar a").removeClass("clicked");
        $("#MenuBar a[name='names']").addClass("clicked");

    });
});

It didn't work and neither did the old ones that used to work 它没有用,过去的老旧也没有用

In a similar question to yours I once found out that only changes in text color are allowed some properties can be changed if you use a:visited pseudo-class (UPD: and background-color is one of them). 与您的问题类似的问题中,我曾经发现仅允许更改文本颜色,如果您使用a:visited伪类(UPD :,并且background-color是其中之一),则可以更改某些属性。 But since your links are javascript links, the :visited selector will not work, hence you cannot do it as a pure CSS solution. 但是由于您的链接是javascript链接,所以:visited选择器将不起作用,因此您不能将其作为纯CSS解决方案来使用。 You will have to use some kind of javascript. 您将必须使用某种javascript。 If jQuery is ok, you can try this: 如果jQuery可以,则可以尝试以下操作:

$('a').on('click', function(){$(this).css("background-color","yellow");});

Perhaps you can change the "showonlyone" function? 也许您可以更改“ showonlyone”功能? Then you could add the background changing code to it. 然后,您可以向其中添加背景更改代码。

You can do this by simple css code: 您可以通过简单的CSS代码执行此操作:

#MenuBar a:visited {
    background: yellow;
}

Edit: 编辑:

As far as this doesn't work with javascript links (but I haven't tried it), there is other solution with jQuery and CSS. 只要这不适用于javascript链接(但我还没有尝试过),jQuery和CSS都有其他解决方案。

jQuery code: jQuery代码:

$(function(){
    $("#MenuBar a").bind("click", function(){
        $(this).addClass("clicked");
    });
});

CSS: CSS:

#MenuBar a.clicked {
    background: yellow;
}

Edit2: 编辑2:

Ok, if you want to keep highlighted only last clicked element, it's enough to add this simple line to javascript code: 好的,如果您只想突出显示最后单击的元素,就可以在JavaScript代码中添加以下简单行:

$(function(){
    $("#MenuBar a").bind("click", function(){
        $("#MenuBar a").removeClass("clicked"); // Remove all highlights
        $(this).addClass("clicked"); // Add the class only for actually clicked element
    });
});

Edit3: 编辑3:

If you want to have more links that point to same location and to highlight all of them if one is clicked, follow this: 如果您希望有更多指向同一位置的链接,并在单击一个链接时突出​​显示所有链接,请遵循以下步骤:

$(function(){
    // Assume that your 'a' elements are in #MenuBar and #colOne
    $("#MenuBar a, #colOne a").bind("click", function(){
        // Remove all highlights
        $("#MenuBar a, #colOne a").removeClass("clicked");

        // Store the name attribute
        var name = $(this).attr("name");

        // Find all elements with given name and highlight them
        $("#MenuBar a[name='"+name+"'], #colOne a[name='"+name+"']").addClass("clicked");
    });
});
$('#MenuBar').on('click', 'a', function() {
    $(this).css('background-color', '#bada55');
});

or if you need unique colors you can use the data-attribute. 或者,如果您需要独特的颜色,则可以使用数据属性。

<a href="#" data-color="#bada55"></a>

$('#MenuBar').on('click', 'a', function() {
    var $elem = $(this);
    $elem.css('background-color', $elem.data('color'));
});

I'd recommended adding classes instead and using css to define styles. 我建议改为添加类,并使用CSS定义样式。

$('#MenuBar').on('click', 'a', function() {
    $(this).addClass('clicked-menu-link');
});

edit: To remove the other clicks use: 编辑:要删除其他点击,请使用:

$('#MenuBar').on('click', 'a', function() {
    var fancyClass = 'clicked-menu-link';
    $('#MenuBar a').removeClass(fancyClass).filter(this).addClass(fancyClass);
});

You can add an active class to the clicked anchor. 您可以将active类添加到单击的锚点。 Using live NodeList should work really fast as you also need to unselect previously selected item: 使用实时NodeList应该非常快速,因为您还需要取消选择以前选择的项目:

var a = document.getElementById('Navigation').getElementsByClassName('active');

$('#Navigation').on('click', 'a', function() {
    if (a[0]) a[0].className = '';
    this.className = 'active';
});

http://jsfiddle.net/vBUCJ/ http://jsfiddle.net/vBUCJ/

Note: getElementsByClassName is IE9+ if you need to support older versions use jQuery: 注意:如果需要支持旧版本,则getElementsByClassName为IE9 +,请使用jQuery:

var $a = $('#Navigation a');
$('#Navigation').on('click', 'a', function() {
    $a.removeClass('active');
    $(this).addClass('active');
});

http://jsfiddle.net/vBUCJ/1/ http://jsfiddle.net/vBUCJ/1/

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

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