使用切换类更改 div 跨度时出现问题

发布于 2024-10-10 06:53:59 字数 319 浏览 0 评论 0原文

我正在使用蓝图 CSS 框架。我有一篇文章跨越 24 列,但我尝试使用 jQuery 切换类 (onclick) 将其减少到 20 列,并在剩余 4 列中显示操作按钮。

$("div").each(function() {
  $(this).click(function() {
   $(this).toggleClass("span-24"); 
   $(this).toggleClass("span-20");
  });
});

我有多个 div,所以我使用每个 div,但它不起作用。

我很感激任何帮助。

I am using blueprint CSS framework. I have an article spanning 24 cols but I trying to use jQuery toggleclass (onclick) to reduce it to 20 cols and show the buttons for actions in the remaining 4 cols.

$("div").each(function() {
  $(this).click(function() {
   $(this).toggleClass("span-24"); 
   $(this).toggleClass("span-20");
  });
});

I have more than one div so I use each, but it does not work.

I appreciate any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

多像笑话 2024-10-17 06:53:59

这段代码应该做你想做的事:

$("div").toggle(function() {
    $(this).attr("class", "span-24");
}, function() {
    $(this).attr("class", "span-20");
});

This code should do what you're after:

$("div").toggle(function() {
    $(this).attr("class", "span-24");
}, function() {
    $(this).attr("class", "span-20");
});
暖心男生 2024-10-17 06:53:59

您可以将点击事件绑定到所有 div,而无需 each 循环。另外,您可以使用 :gt() 大于选择器 然后 toggle() 这些跨度的可见性

$("div").click(function() {
    $(this).find("span:gt(19)").toggle();
});

You can bind the click event to all divs without the each loop. Also, you can use the :gt() greater-than selector and then toggle() the visibility of those spans

$("div").click(function() {
    $(this).find("span:gt(19)").toggle();
});
甜嗑 2024-10-17 06:53:59
  1. 您不想在拥有的所有 div 上切换类,而只想在包含内容的 div 上切换类
  2. 您甚至可以进一步简化此代码:

    $('#toggler').click(function(){
    $('#content').toggleClass('span-20 span-24');
    });

#toggler.click() 只是可以运行 toggleClass() 的事件之一,在 HTML 中它可以是 onload 或其他:

$('#content').toggleClass('span-20 span-24'); //main code (and all of it, too)

例如:http://jsfiddle.net/hhMFs/1/

  1. you don't want to toggle classes on all divs you have, rather just the one with content
  2. you can even simplify this code more:

    $('#toggler').click(function(){
    $('#content').toggleClass('span-20 span-24');
    });

the #toggler.click() is just one of events that can run the toggleClass(), in your HTML it could be onload or whatever:

$('#content').toggleClass('span-20 span-24'); //main code (and all of it, too)

example: http://jsfiddle.net/hhMFs/1/

梦与时光遇 2024-10-17 06:53:59

您还可以这样做:

$("div").click(function() { $(this).toggleClass("span-20 span-24"); });

You can also do this:

$("div").click(function() { $(this).toggleClass("span-20 span-24"); });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文