gpt4 book ai didi

jquery - 我的 jQuery 插件有弱点(模式方面)吗?

转载 作者:行者123 更新时间:2023-12-01 06:07:15 27 4
gpt4 key购买 nike

我在互联网上找到了一个插件模式。 (我一找到它就会提交链接)并且我对其进行了一些修改以创建我自己的对话框插件。恐怕即使代码可以工作,我的制作方式也没有意义。

我必须:

  • 能够将我的插件分配给多个元素(多于一个对话框)-已实现
  • 我必须能够访问它的方法:打开对话框,关闭对话框,从其范围之外分配选项-尚未实现
  • 我想发送引用信息至当有按钮时当前对话框发生点击 - 部分实现。我正在使用 $(this).getDialog()方法。 this 指的是点击的按钮

这是插件:

(function($) {
var pluginName = "Dialog",
dialogContent = { "Title" : "h2", "SubTitle" : "p.sub-title", "Body" : "div.content" }

var infDialog = function( el, options ) {
var $el = $(el),
currentConfig = {
position : [100, 100],
autoOpen : true
};

$el.data( pluginName, $el);

if ( options ) {
currentConfig = $.extend( currentConfig, options );
}

$el.css({ "top" : currentConfig.position[1], "left" : currentConfig.position[0], "z-index" : 9999 });

if ( currentConfig.autoOpen ) {
$el.fadeIn( "slow" );
}

if ( currentConfig.buttons ) {
$.each(currentConfig.buttons, function(i, j) {
if ( $.isFunction( j ) && $(el).find("input[value='" + i + "']").length )
{
var $currentButton = $(el).find("input[value='" + i + "']");

$(el).find("input[value='" + i + "']").click(function() {
j.call( $currentButton );
});
}
});
}

if ( currentConfig.onOpen ) {
currentConfig.onOpen.call( $el );
}

if ( currentConfig.onClose ) {
$el.bind("onclose", function() {
currentConfig.onClose.call( $el );
});
}

$el.getDialog().bind("click", function( e ) {
var currentDialog = this.id,
currentPosition = $(this).css("z-index");

if ( currentPosition < 9999 || currentPosition == "auto" ) {
$(".dialog").each(function(i) {
if ( this.id == currentDialog ) {
$(this).css("z-index", 9999);
} else {
$(this).css("z-index", 9990);
}
});

$(this).css("z-index");
}
});
}

$.fn.infDialog = function( options ) {
return this.each(function() {
( new infDialog( this, options ) );
});
}

$.fn.closeDialog = function() {
return $(this).getDialog().fadeOut("slow", function() {
$(this).trigger("onclose");
});
}

$.fn.getDialog = function() {
return ( ! $(this).is(".dialog") ) ? $(this).closest(".dialog") : $(this);
}

$.fn.assignOption = function( options ) {
var $currentPlugin = $(this);

$.each( options, function(i, j) {
if ( dialogContent[ i ] ) {
$currentPlugin.find( dialogContent[ i ] ).empty().html( j );
}
});
}
})(jQuery);

和对话框的 HTML :

<div id="dialogTest" class="dialog">
<div>
<h2>title</h2>
<p class="sub-title">
subtitle
</p>
<div class="content">
Content
</div>
<p class="buttons"><input type="button" value="Action" /> <input type="button" value="Close" class="normal" /></p>
</div>
</div>

和 jQuery 代码:

$("#dialogTest").infDialog({
position : [400, 190],
buttons : {
"Action" : function() {
var $dialog = $(this).getDialog(),
obj = $dialog.data("Dialog"),
$currentDialog = $(this).getDialog();

$currentDialog.assignOption({
"Title" : "New Title",
"SubTitle" : "Lorem ipsum",
"Bob" : "unknown body tag",
"Body" : "testbody"
});

$(this).attr("value", Math.random());
},
"Close" : function() {
$(this).closeDialog();
}
},
onOpen : function() {

},
onClose : function() {
var $currentDialog = $(this).getDialog();

$currentDialog.fadeIn("fast");
}
});

我做错了什么,还是我实际上正在朝着好的方向发展?

顺便说一句,我发现设计模式建议的这段代码: $el.data(pluginName, $el); 不起作用。事实上,每次我尝试使用 $("#dialogTest").data("Dialog") 检索对象时,返回的对象都是空的。

谢谢

最佳答案

jQuery Plugin Authoring tutorial给了我我使用的 jQuery 插件模式。

关于您的assignOptions任务...

如果您有一个私有(private)设置对象并向您的插件传递选项,它会很好地工作(当然,这在教程中进行了概述)。

扩展示例

(function( $ ){

$.fn.tooltip = function( options ) {
//private settings
var settings = {
'location' : 'top',
'background-color' : 'blue'
};
// returning this.each ensures your plugin works on multiple matched elements
return this.each(function() {
// If options exist, lets merge them
// with our default settings
if ( options ) {
$.extend( settings, options );
}

// Tooltip plugin code here

});

};
})( jQuery );
//initiate plugin with an options object
var options = { 'location' : 'left' };
$('div').tooltip( options );

关于jquery - 我的 jQuery 插件有弱点(模式方面)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4467615/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com