js代码来替换复选框和闭包
以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我更改了几个项目并为此创建了一个 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 toconsole.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 withinsert
. 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
betweenlabel
and the next checkbox, maybe you need to do something like that.