js代码来替换复选框和闭包

发布于 2024-11-06 05:06:03 字数 1179 浏览 0 评论 0原文

以下 javascript(原型 1.6)代码隐藏页面上的所有复选框,并插入具有某种 css 样式的 div 元素和一个单击事件来充当假复选框。它还会查找复选框旁边(或前面)的标签,以触发相同的事件。

当我单击 div (fake_el) 本身时,一切都会按预期工作,但是当我对标签尝试相同的操作时,它只能工作一次。之后, el 不会改变 - 就好像它(el)将是一个值参数。

这里有什么想法吗?

    $$('input[type=checkbox]').each(function(el) {
        if (el.visible()) {
            var fake_el = new Element('div', { className:'checkbox checkbox_' + el.checked });
            var label = (el.next() != null && el.next().tagName === 'LABEL' && el.next().readAttribute('for') === el.id) ? el.next() : undefined;
            label = (el.previous() != null && el.previous().tagName === 'LABEL' && el.previous().readAttribute('for') === el.id) ? el.previous() : label;
            var action = function(el) {
                el.checked = (el.checked) ? false : true;
                fake_el.className = 'checkbox checkbox_' + el.checked;
            }

            fake_el.observe('click', function() { action(el); });
            if (label != null) { label.observe('click', function() { c.log(el); action(el); c.log(el); }); }
            el.insert({ after:fake_el }).hide();
        }
    });

The following javascript (prototype 1.6) code hides all checkboxes on the page and inserts a div element with some css style and a click event to act as a fake-checkbox. It also looks out for a label next (or previous) the checkbox, to also trigger the same event.

When I click the div (fake_el) itself, everything works as expected, but when I try the same with the label, it only works one time. after that, the el isn't gonna change - as if it (the el) would be a value-parameter.

Any ideas here?

    $('input[type=checkbox]').each(function(el) {
        if (el.visible()) {
            var fake_el = new Element('div', { className:'checkbox checkbox_' + el.checked });
            var label = (el.next() != null && el.next().tagName === 'LABEL' && el.next().readAttribute('for') === el.id) ? el.next() : undefined;
            label = (el.previous() != null && el.previous().tagName === 'LABEL' && el.previous().readAttribute('for') === el.id) ? el.previous() : label;
            var action = function(el) {
                el.checked = (el.checked) ? false : true;
                fake_el.className = 'checkbox checkbox_' + el.checked;
            }

            fake_el.observe('click', function() { action(el); });
            if (label != null) { label.observe('click', function() { c.log(el); action(el); c.log(el); }); }
            el.insert({ after:fake_el }).hide();
        }
    });

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

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

发布评论

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

评论(1

花心好男孩 2024-11-13 05:06:03

我更改了几个项目并为此创建了一个 jsFiddle 。首先也是最重要的,c.log 必须更改为 console.log 才能为我工作:)。之后,我唯一改变的是 div 的添加方式,因为它不适用于我的 insert。我设置了一些测试数据,然后我就走了...

编辑:也许您在两个复选框之间没有非标签标签,并且它变得混乱?请注意,我在 label 和下一个复选框之间有一个 br,也许您需要执行类似的操作。

I changed a couple items and created a jsFiddle for this. First and foremost, c.log had to be changed to console.log for it to work for me :). After that, the only thing I changed was how the divs were added, since it wasn't working for me with insert. I set up some test data and away I went...

EDIT: Perhaps you don't have a non-label tag between two checkboxes and it is getting confused? Notice I have a br between label and the next checkbox, maybe you need to do something like that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文