我如何使用 Javascript 切换隐藏或显示 div 和类?

发布于 2024-10-16 07:06:09 字数 80 浏览 1 评论 0原文

嗨,我只是希望能够通过类分别切换隐藏/显示 div。

我的 div 是 ul.post_controls

Hi i just want to be able to toggle to hide/show a div respectively with a class.

my div is ul.post_controls

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

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

发布评论

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

评论(4

是你 2024-10-23 07:06:09

编辑 抱歉,我没有意识到您没有提到 jQuery - 它听起来很像一个 jQuery 问题。同时,如果您正在考虑使用像 jQuery 这样的 JavaScript 框架,请查看这里的生活有多简单:http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery


您几乎已经说过了:

$('ul.post_controls').toggle();

这是参考:http://api.jquery.com/toggle/


这就是我设置它的方式,以便在单击按钮时 uls 切换:

$(document).ready(function () { // Need DOM to be ready
    $('#myButton').click(function () { // Attach click event handler
        $('ul.post_controls').toggle(); // Toggle the ul when clicked
    });
});

假设您有一个 ID 为“myButton”的元素。

Edit Sorry didn't realise you didn't mention jQuery - it just sounded so much like a jQuery question. At the same time, if you are considering using a JavaScript framework like jQuery, check out how much simpler life can be here: http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery.


You almost said it:

$('ul.post_controls').toggle();

Here's the reference: http://api.jquery.com/toggle/.


This is how I'd set it up so the uls toggle when a button is clicked:

$(document).ready(function () { // Need DOM to be ready
    $('#myButton').click(function () { // Attach click event handler
        $('ul.post_controls').toggle(); // Toggle the ul when clicked
    });
});

That's assuming you have an element with an ID of "myButton".

晨曦慕雪 2024-10-23 07:06:09
/*
 * cssjs
 * written by Christian Heilmann (http://icant.co.uk)
 * eases the dynamic application of CSS classes via DOM
 * parameters: action a, object or name o and class names c1 and c2 (c2 optional)
 * actions: swap exchanges c1 and c2 in object o
 *          add adds class c1 to object o
 *          remove removes class c1 from object o
 *          toggle turns class c1 off if it is currently on and vice-versa
 *          check tests if class c1 is applied to object o
 * example: cssjs('swap',document.getElementById('foo'),'bar','baz');
 */

function cssjs(a,o,c1,c2)
{
    if (o) {
        switch (a){
            case 'swap':
                o.className=!cssjs('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
                break;
            case 'add':
                if(!cssjs('check',o,c1)){o.className+=o.className?' '+c1:c1;}
                break;
            case 'remove':
                var rep=o.className.match(' '+c1)?' '+c1:c1;
                o.className=o.className.replace(rep,'');
                break;
            case 'toggle':
                cssjs('check',o,c1) ? cssjs('remove',o,c1) : cssjs('add',o,c1);
                break;      
            case 'check':
                return new RegExp('\\b'+c1+'\\b').test(o.className);
        }
    }
}o
/*
 * cssjs
 * written by Christian Heilmann (http://icant.co.uk)
 * eases the dynamic application of CSS classes via DOM
 * parameters: action a, object or name o and class names c1 and c2 (c2 optional)
 * actions: swap exchanges c1 and c2 in object o
 *          add adds class c1 to object o
 *          remove removes class c1 from object o
 *          toggle turns class c1 off if it is currently on and vice-versa
 *          check tests if class c1 is applied to object o
 * example: cssjs('swap',document.getElementById('foo'),'bar','baz');
 */

function cssjs(a,o,c1,c2)
{
    if (o) {
        switch (a){
            case 'swap':
                o.className=!cssjs('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
                break;
            case 'add':
                if(!cssjs('check',o,c1)){o.className+=o.className?' '+c1:c1;}
                break;
            case 'remove':
                var rep=o.className.match(' '+c1)?' '+c1:c1;
                o.className=o.className.replace(rep,'');
                break;
            case 'toggle':
                cssjs('check',o,c1) ? cssjs('remove',o,c1) : cssjs('add',o,c1);
                break;      
            case 'check':
                return new RegExp('\\b'+c1+'\\b').test(o.className);
        }
    }
}o
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文