【发布时间】:2014-07-22 08:35:10
【问题描述】:
为什么在未选中复选框时,下面的 HTML/JS 会返回“on”值?此代码(目前)在 tab.my 上有效。
HTML
<button type="button" id="init" class="btn btn-primary btn-sm">Init</button>
<form id="domains">
<div id="domain1">
<input type="checkbox" name="input1" />
<a>Domain 1</a>
</div>
<div id="domain2">
<input type="checkbox" name="input2" />
<a>Domain 2</a>
</div>
</form>
JS
$('init').observe('click', init);
function init() {
$('domains').select('div').each(function(domain){
console.log(domain.select('input')[0].value);
});
}
我阅读了this 的帖子,但我正在尝试学习有关 JavaScript 性能的最佳实践,并获得更多基础知识。我的猜测是,您在 select() 方法中使用 $ 实用程序的次数越多,使用 $$ 实用程序的次数越少,它所循环的次数就越少。下面的函数返回了正确的值,但我需要访问每个“域 div”中的其他元素。
function init() {
$('domains').getElements().each(function(input){
console.log($F(input));
});
}
我想也许我应该使用 $F 实用程序,但是下面的函数返回了 JS 错误
未捕获的类型错误:无法读取未定义的属性“toLowerCase”
function init() {
$('domains').select('div').each(function(domain){
console.log($F(domain.select('input')));
});
}
【问题讨论】:
标签: javascript html performance prototypejs