我有一个用于删除文本的小jquery插件。问题是,现在我需要在dinamically内容(使用淘汰赛)中使用此插件。如果在页面中加载这个插件,我怎么能在页面的所有元素中使用它?
我的插件:
(function ($){
$.fn.ellipsiText= function(options) {
var settings = $.extend({
length : 30,
ellipsi : "..."
}, options );
return this.each(function() {
var elem = $(this);
var txt = elem.text();
if (txt.length>settings.length) {
elem.text(txt.substr(0,settings.length - settings.ellipsi.length) + settings.ellipsi );
}
});
};
答案 0 :(得分:1)
相同的功能,您可以转换为淘汰赛自定义绑定。
ellipsiText绑定: -
ko.bindingHandlers.ellipsiText = {
init: function (element, valueAccessor, allBindingsAccessor) {
$el = $(element);
ko.utils.registerEventHandler(element, "change", function () {
var observable = valueAccessor();
observable($el.val());
});
},
update: function (element, valueAccessor, allBindingsAccessor, context) {
var options = allBindingsAccessor().ellipsiTextOptions || {};
var settings = $.extend({
length: 30,
ellipsi: "..."
}, options);
var value = valueAccessor();
var $element = $(element);
var txt = value();
if (txt.length > settings.length) {
$element.text(txt.substr(0, settings.length - settings.ellipsi.length) + settings.ellipsi);
}else {
$element.text(txt);
}
}
};