Javascript jquery切换ID而不是类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7394610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:06:52  来源:igfitidea点击:

jquery toggle id's instead of classes?

javascriptjquerycss

提问by AlexW.H.B.

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?

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

$("#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;
       });

采纳答案by Derek Adair

EDIT:

编辑:

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.

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

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

这是您可能正在寻找的 jQuery 片段的本质:

$("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');
}); 

Demo

演示



Check out the jQuery toggleapi.

查看 jQuery切换api。

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.

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

like so...

像这样...

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

回答by tere?ko

You really should use classes for that. IDs are unique within a page and should be used as points where you catch events ( via $.live()or some other method which uses event delegation ). Besides , if you think of using IDs because they have higher specificity in CSS rules , then you are going the wrong way.

你真的应该为此使用类。ID 在页面内是唯一的,应该用作捕获事件的点(通过$.live()或其他使用事件委托的方法)。此外,如果您认为使用 ID 是因为它们在 CSS 规则中具有更高的特异性,那么您就走错了路。

In short: bad idea, stick to toggling classes.

简而言之:坏主意,坚持切换课程。

回答by Jishan mondal

jQuery doesnt has toggleId() function . But you can create your own id toggle function .

jQuery 没有 toggleId() 函数。但是您可以创建自己的 id 切换功能。

function toggleId(className,id)
{
    var tag = document.querySelector('.'+className);
    tag.id = tag.getAttribute("id") ? '' : id;
}

toggleId("myClass","id");

this will toggle id ( id and NULL ) of myClass element .

这将切换 myClass 元素的 id(id 和 NULL)。

another example for toggling between two id

在两个 id 之间切换的另一个例子

suppose you want to toggle between to id ( id1 and id2 ) of an element then

假设您想在元素的 id( id1 和 id2 )之间切换,然后

function toggleId(className,id1,id2)
   {
       var tag = document.querySelector('.'+className);
       tag.id = tag.getAttribute("id") ? id2 : id1;
   }

toggleId("myClass","id1","id2");