问题描述
在此对象内,我有一个属性Response.action
,该属性旨在同时触发jQuery的.ready
和.resize
上的代码.下面的代码块中的注释演示了其用法. Response.action
在.ready
上有效,但在.resize
上无效.谁能看到原因和/或建议如何使其同时适用于两者?
Inside this object, I have a property Response.action
that is meant to be a shorthand for triggering code on jQuery's .ready
and .resize
simultaneously. The comment in the code block below demonstrates its usage. Response.action
works on .ready
but not on .resize
. Can anyone see why and/or suggest how to make it work for both?
window.Response = (function($, window, undefined) {
var Response = {}, // object
$window = $(window),
$document = $(document); // cache selectors
/*
Response.action()
This triggers code on .ready *and* .resize
usage:
Response.action( myactions );
function myactions() {
// do stuff
}
*/
action = function( code ) {
var code = ( code !== undefined ) ? wrap() : false; // apply wrap() if we have code
function wrap() {
$document.ready(function() {
$window.resize(function() {
code // input
}).resize(); // trigger resize handlers
}); // close .ready function
}
return code; // wrapped code fires on .ready and .resize
},
Response.action = action;
return Response; // return object
})(jQuery, window); // expose to global object
这是针对 responsejs.com 的-完整的lib(进行中)在那里.
This is for responsejs.com - the full lib (in progress) is there.
我正在使用其他属性之一进行测试. .band
属性本身是可靠的:
I'm using one of the other properties to test it. The .band
property is solid on its own:
Response.action( myactions() );
function myactions() {
if ( Response.band(600) ) { $('header').html('600px or wider'); }
else { $('header').html('below 600px'); }
}
更新:有效:
Response.action =函数(func){ if(typeof func!=='function'){返回false; }
Response.action = function ( func ) { if ( typeof func !== 'function' ) { return false; }
$(function () {
func();
$window.resize( func );
}).resize();
return func;
};
具有以下用法语法:
Response.action( myactions );
function myactions() {
// do stuff
}
*请注意,在通话中,它必须是myactions
而不是myactions()
*Note that in the call it needs to be myactions
as opposed to myactions()
推荐答案
如何
window.Response = (function ( $, window, undefined ) {
var Response = {};
Response.action = function ( func ) {
if ( typeof func !== 'function' ) { return false; }
$(function () {
func();
$( window ).resize( func );
});
return func;
};
return Response;
})( jQuery , window );
这篇关于将.ready和.resize链接成函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!