我找到了一种方法来做到这一点,但不幸的是它涉及Bootstrap源代码的更改。的代码段完成实际方法调用是这样的:
$.fn.modal = function (option) {
return this.each(function() {
var $this = $(this)
, data = $this.data('modal')
, options = $.extend({}, $.fn.modal.defaults, $this.data(), typeof option == 'object' && option)
if (!data) $this.data('modal', (data = new Modal(this, options)))
if (typeof option == 'string') data[option]()
else if (options.show) data.show()
})
}
为了改变这种情况,第7行(在source code线206)应该被修饰以通过最初传递给封闭函数的任何其他参数。另外,必须将原始参数赋予jQuery的.each()
函数的每个迭代。这里的工作代码:
$.fn.modal = function (option) {
return this.each(function() {
var $this = $(this)
, data = $this.data('modal')
, options = $.extend({}, $.fn.modal.defaults, $this.data(), typeof option == 'object' && option)
if (!data) $this.data('modal', (data = new Modal(this, options)))
if (typeof option == 'string') data[option].apply($this.data('modal'), Array.prototype.slice.call(arguments, 1)); // pass the parameters on
else if (options.show) data.show()
}, arguments) // execute each iteration with the original parameters
}
我仍然在尝试,以确保这种变化不会产生任何不良的副作用,但到目前为止,一切正常。任何更优雅的解决方案将受到欢迎。