所以一切都运行正常,直到进行ajax调用并调用该函数以在新更新的文本上运行。我理解这是因为它是一个.each()而不是.live(),但是我不知道如何让这个函数对ajax中新更新的文本起作用。
html&的CSS:
.ellipsis {
white-space: nowrap;
overflow: hidden;
}
.ellipsis.multiline {
white-space: normal;
}
<div class="ellipsis" style="width: 100px; border: 1px solid black;">Lorem ipsum dolor sit amet, consectetur adipisicing elit</div>
<div class="ellipsis multiline" style="width: 100px; height: 40px; border: 1px solid black; margin-bottom: 100px">Lorem ipsum dolor sit amet, consectetur adipisicing elit</div>
<script type="text/javascript" src="/js/jquery.ellipsis.js"></script>
<script type="text/javascript">
$(".ellipsis").ellipsis();
</script>
jquery.ellipsis.js(该函数是这个省略号):
(function($) {
$.fn.ellipsis = function()
{
return this.each(function()
{
var el = $(this);
if(el.css("overflow") == "hidden")
{
var text = el.html();
var multiline = el.hasClass('multiline');
var t = $(this.cloneNode(true))
.hide()
.css('position', 'absolute')
.css('overflow', 'visible')
.width(multiline ? el.width() : 'auto')
.height(multiline ? 'auto' : el.height())
;
el.after(t);
function height() { return t.height() > el.height(); };
function width() { return t.width() > el.width(); };
var func = multiline ? height : width;
while (text.length > 0 && func())
{
text = text.substr(0, text.length - 1);
t.html(text + "...");
}
el.html(t.html());
t.remove();
}
});
};
})(jQuery);
然而,一旦将省略号div中的文本更新为ajax成功回调中的任何其他内容,例如“嗨,我是这个div中的新文本并且应该被修剪”,这在文本上工作正常。这是行不通的。我希望它仅应用于成功回调中新更新的文本。
这是我到目前为止所尝试的内容:
success: function(data) {
$(".ellipsis").text(data);
$(".ellipsis", data).ellipsis(); // not working
}
这是没有ajax http://jsfiddle.net/TF6Rb/625/
的小提琴有什么想法吗?
答案 0 :(得分:2)
做$(".ellipsis").ellipsis();
我应该工作正常。