本文介绍了jquery切换ID而不是类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法制作一个切换功能,首先只切换一个 css 样式元素,例如背景颜色或类似的东西.并且选择一个 id 而不是一个类,因为我知道toggleClass,但我只是想知道是否可以用 ids 代替?

is there a way to make a toggle function that first of all toggles only one css style element, such as background-color or something like that. and that selects an id instead of a class, for i know of the toggleClass, but im just wondering if it's possible with ids instead?

$("#gallery").load('http://localhost/index.php/site/gallerys_avalible/ #gallerys_avalible');
  $('li').live('click', function(e) {
      e.preventDefault();
      if(!clickCount >=1) {
        $(this).css("background-color","#CC0000");
        clickCount++;
        }
       console.log("I have been clicked!");
       return false;
       });

推荐答案

阅读 OP 的评论后 - 我相信这就是他正在寻找一种在点击时突出显示活动"链接的方法.是的,teresko 绝对正确,您应该切换类,而不是 ID.

After reading OP's comment - I believe this is what he is looking for a way to highlight an "active" link on click. And Yes, teresko is definitely right that you should be toggling the classes, not the ID's.

这是您可能正在寻找的 jQuery 片段的精髓:

This is the essence of a jQuery snippet that you may be looking for:

$("li").bind('click', function(){
   // remove the active class if it's there
   if($("li.active").length) $("li.active").removeClass('active');
   // add teh active class to the clicked element
   $(this).addClass('active');
});

演示

这有点令人困惑,因为在 jQuery 切换上的简单谷歌搜索会将您带到显示/隐藏切换文档.但是,.toggle() 可用于替代功能 - 您甚至可以添加两个以上.

It's a little confusing because a simple google search on jQuery toggle brings you to the show/hide toggle documentation. But, .toggle() can be used to alternate functions - you can even add more than two.

$("el").toggle(
       function(){
          $(this).css('background-color', 'red');
       },
       function(){
           $(this).css('background-color, ''); // sets the bg-color to nothing
       });

这篇关于jquery切换ID而不是类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 05:40