gpt4 book ai didi

javascript - 在 jquery 库中重新定义方法的最佳方式

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:35:33 25 4
gpt4 key购买 nike

假设我想更改 bootstrap-typeahead 库中的渲染函数。 http://twitter.github.com/bootstrap/assets/js/bootstrap-typeahead.js

激活此目标的一种方法是:

_.extend($.fn.typeahead.Constructor.prototype, {
render: function (items) {
// some code
}
});

假设我想为特定元素重新定义渲染函数,例如...

element.typeahead({
minLength: 3,
source: getSource
});

我应该如何重新定义渲染函数才能只对这个元素有效?

附言:
我正在使用 jquery 和下划线

最佳答案

也许你可以先克隆它?

$.fn.typeaheadCustom = $.fn.typeahead;

_.extend($.fn.typeaheadCustom.Constructor.prototype, {
render: function (items) {
// some code
}
});

element.typeaheadCustom({
minLength: 3,
source: getSource
});

更新

您可能需要深度克隆它:

$.fn.typeaheadCustom = $.extend(true, $.fn.typeahead, {});

关于javascript - 在 jquery 库中重新定义方法的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12194171/

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