jquery 函数到插件:返回每个结果的问题
我在将下面的函数转换为插件时遇到问题,你能告诉我如何修复它吗?
功能,
function highest_Zindex(){
var index_highest = 0;
// more effective to have a class for the div you want to search and
// pass that to your selector
$("div").each(function() {
if($(this).css("zIndex") > 0)
{
// always use a radix when using parseInt
var index_current = parseInt($(this).css("zIndex"), 10);
if(index_current > index_highest) {
index_highest = index_current;
}
}
});
return index_highest;
}
它工作完美,
var test = highest_Zindex();
alert(test);
插件,
(function($){
$.fn.extend({
get_highest_Zindex: function(options) {
var defaults = {
increment: 10 // The opacity of the background layer.
}
var options = $.extend(defaults, options);
var $cm = this.each(function(){
var o = options;
var object = $(this); // always return #document.
var index_highest = 0;
if(object.css("zIndex") > 0)
{
// always use a radix when using parseInt
var index_current = parseInt(object.css("zIndex"), 10);
//alert(index_current);
if(index_current > index_highest) {
index_highest = index_current;
}
}
});
return index_highest;
}
});
})(jQuery);
然后我会收到错误消息,
var test = $("div").get_highest_Zindex();
alert(test);
index_highest 未定义
什么想法吗?
谢谢。
I am having a problem turning the function below into a plugin, could you please tell me how to fix it?
the function,
function highest_Zindex(){
var index_highest = 0;
// more effective to have a class for the div you want to search and
// pass that to your selector
$("div").each(function() {
if($(this).css("zIndex") > 0)
{
// always use a radix when using parseInt
var index_current = parseInt($(this).css("zIndex"), 10);
if(index_current > index_highest) {
index_highest = index_current;
}
}
});
return index_highest;
}
it works perfectly,
var test = highest_Zindex();
alert(test);
the plugin,
(function($){
$.fn.extend({
get_highest_Zindex: function(options) {
var defaults = {
increment: 10 // The opacity of the background layer.
}
var options = $.extend(defaults, options);
var $cm = this.each(function(){
var o = options;
var object = $(this); // always return #document.
var index_highest = 0;
if(object.css("zIndex") > 0)
{
// always use a radix when using parseInt
var index_current = parseInt(object.css("zIndex"), 10);
//alert(index_current);
if(index_current > index_highest) {
index_highest = index_current;
}
}
});
return index_highest;
}
});
})(jQuery);
then I will get the error message,
var test = $("div").get_highest_Zindex();
alert(test);
index_highest is not defined
Any ideas?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需将
var index_highest = 0;
移至each
语句之外即可:Simply move
var index_highest = 0;
outside of theeach
statement: