避免 jQuery 中函数的重复
该函数编写了两次,以保存复选框状态以及相应的 div 文本。如何在函数中编写一次代码,然后分别在加载和单击事件时调用它两次?
$(document).ready(function() {
$("#bquote").load("quotes_in.php", function() {
// a key prefix is used for the cookie/storage
var storedData = getStorage('com_mysite_checkboxes_');
$('div.check input:checkbox').bind('change',function(){
$('#ab_' + this.id).toggle($(this).is(':checked'));
// save the data on change
storedData.set(this.id, $(this).is(':checked')?'checked':'not');
}).each(function() {
// on load, set the value to what we read from storage:
var val = storedData.get(this.id);
if (val == 'checked') $(this).attr('checked', 'checked');
if (val == 'not') $(this).removeAttr('checked');
if (val) $(this).trigger('change');
});
});
});
$(function() {
/*load quotes on click of link*/
$("a#main")
.click(function() {
$(this).addClass("current");
$("#bquote").load("quotes_in.php", function() {
// a key prefix is used for the cookie/storage
var storedData = getStorage('com_mysite_checkboxes_');
$('div.check input:checkbox').bind('change',function(){
$('#ab_' + this.id).toggle($(this).is(':checked'));
// save the data on change
storedData.set(this.id, $(this).is(':checked')?'checked':'not');
}).each(function() {
// on load, set the value to what we read from storage:
var val = storedData.get(this.id);
if (val == 'checked') $(this).attr('checked', 'checked');
if (val == 'not') $(this).removeAttr('checked');
if (val) $(this).trigger('change');
});
});
});
This function is written twice to save the checkbox state as well as the corresponding div text. How do I write the code in a function once and then call it twice on load and click events respectively?
$(document).ready(function() {
$("#bquote").load("quotes_in.php", function() {
// a key prefix is used for the cookie/storage
var storedData = getStorage('com_mysite_checkboxes_');
$('div.check input:checkbox').bind('change',function(){
$('#ab_' + this.id).toggle($(this).is(':checked'));
// save the data on change
storedData.set(this.id, $(this).is(':checked')?'checked':'not');
}).each(function() {
// on load, set the value to what we read from storage:
var val = storedData.get(this.id);
if (val == 'checked') $(this).attr('checked', 'checked');
if (val == 'not') $(this).removeAttr('checked');
if (val) $(this).trigger('change');
});
});
});
$(function() {
/*load quotes on click of link*/
$("a#main")
.click(function() {
$(this).addClass("current");
$("#bquote").load("quotes_in.php", function() {
// a key prefix is used for the cookie/storage
var storedData = getStorage('com_mysite_checkboxes_');
$('div.check input:checkbox').bind('change',function(){
$('#ab_' + this.id).toggle($(this).is(':checked'));
// save the data on change
storedData.set(this.id, $(this).is(':checked')?'checked':'not');
}).each(function() {
// on load, set the value to what we read from storage:
var val = storedData.get(this.id);
if (val == 'checked') $(this).attr('checked', 'checked');
if (val == 'not') $(this).removeAttr('checked');
if (val) $(this).trigger('change');
});
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将其声明为命名函数一次并调用它两次,如下所示:
我还将
$(this).is(':checked')
更改为this.checked
在上面,不需要让它变慢,DOM 属性在这里起作用:)You can declare it once as a named function and call it twice, like this:
I also changed
$(this).is(':checked')
tothis.checked
in the above, no need to make it slower, the DOM property works here :)