JQuery 代码对 Google Chrome 没有影响,但在 Firefox 中运行良好
我使用一段 JQuery 代码和一些 Mootools 来更改某些输入的类,但问题是它在 Google Chrome 中不起作用(没有效果),尽管它在 Firefox 中工作得很好。
编辑:我发现,如果我在页面上的给定元素上单击两次,该类就会被有效添加。
我的代码
<script type="text/javascript">
window.addEvent('domready', function(){
var myCheck = new FormCheck('formulaire');
$('votconj').addEvent('click', function() {
// code inside works perfectly
});
$('votconj_no').addEvent('click', function() {
// code inside works perfectly
});
$('nb_children').addEvent('click', function() {
var selected = $('nb_children').getSelected();
selected.each(function(element) {
var val = element.get('value');
for (var counter = 1; counter <= val; counter++) {
$('jj_enfant' + counter).addClass("validate['required']");
myCheck.register($('jj_enfant' + counter));
$('mm_enfant' + counter).addClass("validate['required']");
myCheck.register($('mm_enfant' + counter));
$('aaaa_enfant' + counter).addClass("validate['required']");
myCheck.register($('aaaa_enfant' + counter));
$('last_name_enfant' + counter).addClass("validate['required','nodigit']");
myCheck.register($('last_name_enfant' + counter));
$('first_name_enfant' + counter).addClass("validate['required','nodigit']");
myCheck.register($('first_name_enfant' + counter));
// Here the good value is displayed
alert(val);
}
for (var counter = parseInt(val)+1; counter <= 6; counter++) {
$('jj_enfant' + counter).removeAttribute('class');
myCheck.dispose($('jj_enfant' + counter));
$('mm_enfant' + counter).removeAttribute('class');
myCheck.dispose($('mm_enfant' + counter));
$('aaaa_enfant' + counter).removeAttribute('class');
myCheck.dispose($('aaaa_enfant' + counter));
$('last_name_enfant' + counter).removeAttribute('class');
myCheck.dispose($('last_name_enfant' + counter));
$('first_name_enfant' + counter).removeAttribute('class');
myCheck.dispose($('first_name_enfant' + counter));
}
});
});
})
...
</script>
I use a piece of JQuery code with a bit of Mootools to change the class of some inputs but the thing is that it doesn't work (has no effect) in Google Chrome despite working perfectly in Firefox.
Edit : I've discovered that if I click two times on the given element on the page the class gets effectively added.
my code
<script type="text/javascript">
window.addEvent('domready', function(){
var myCheck = new FormCheck('formulaire');
$('votconj').addEvent('click', function() {
// code inside works perfectly
});
$('votconj_no').addEvent('click', function() {
// code inside works perfectly
});
$('nb_children').addEvent('click', function() {
var selected = $('nb_children').getSelected();
selected.each(function(element) {
var val = element.get('value');
for (var counter = 1; counter <= val; counter++) {
$('jj_enfant' + counter).addClass("validate['required']");
myCheck.register($('jj_enfant' + counter));
$('mm_enfant' + counter).addClass("validate['required']");
myCheck.register($('mm_enfant' + counter));
$('aaaa_enfant' + counter).addClass("validate['required']");
myCheck.register($('aaaa_enfant' + counter));
$('last_name_enfant' + counter).addClass("validate['required','nodigit']");
myCheck.register($('last_name_enfant' + counter));
$('first_name_enfant' + counter).addClass("validate['required','nodigit']");
myCheck.register($('first_name_enfant' + counter));
// Here the good value is displayed
alert(val);
}
for (var counter = parseInt(val)+1; counter <= 6; counter++) {
$('jj_enfant' + counter).removeAttribute('class');
myCheck.dispose($('jj_enfant' + counter));
$('mm_enfant' + counter).removeAttribute('class');
myCheck.dispose($('mm_enfant' + counter));
$('aaaa_enfant' + counter).removeAttribute('class');
myCheck.dispose($('aaaa_enfant' + counter));
$('last_name_enfant' + counter).removeAttribute('class');
myCheck.dispose($('last_name_enfant' + counter));
$('first_name_enfant' + counter).removeAttribute('class');
myCheck.dispose($('first_name_enfant' + counter));
}
});
});
})
...
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不使用 jQuery Ready() 来检测 Dom 何时准备就绪。
jQuery 旨在以跨浏览器的方式检测此事件,
替换
为
why don't you use jQuery ready() for detecting when then the Dom is ready.
jQuery is intended to detect this event in a cross browser way
replace
with
Firefox 6 或 Chrome 13 中都没有
addEvent
方法,至少在标准合规模式下没有。 (也许您想使用addEventListener
方法?)如果您使用 Quirks 模式,某些浏览器支持一些非标准方法,但如果您希望它跨浏览器工作,则不能使用这些方法。
请改用 jQuery 中的
ready
事件,该事件还为不支持该事件的浏览器(即 IE)模拟domready
事件。绑定事件处理程序的 jQuery 方法名为
bind
,而不是addEvent
。您还可以使用click
方法来绑定点击事件。请注意,HTML 中没有
votconj
元素,因此上面的选择器不匹配任何内容。There is no
addEvent
method, neither in Firefox 6 or Chrome 13, at least not in Standards Compliance Mode. (Perhaps you wanted to use theaddEventListener
method?)If you are using Quirks Mode some browsers are supporting some non-standard methods, but you can't use those methods if you want it to work cross browser.
Use the
ready
event in jQuery instead, which also emulates thedomready
event for browsers that doesn't support it (i.e. IE).The jQuery method to bind event handlers is named
bind
, notaddEvent
. You can also use theclick
method to bind the click event.Note that there is no
votconj
element in HTML, so the selector above doesn't match anything.以下代码效果很好(我已将事件从单击更改为模糊,并且通过仅获取所选数组的最后一个元素,使代码变得更加简单和清晰)。
The following code works great (I've changed the event from click to blur and I've made the code a bit simpler and cleaner by getting only the last element of the array selected).