2014-02-07 80 views
0
$('document').ready(function(){ 
    //textoverflow($('.content'),100); 

    $('span').click(function(){ 
     //disable textoverflow function & output full text 
    }); 
}); 

function textoverflow(ele, num){ 
    ele.each(function() { 
     $(this).text(
      $(this).text().slice(0,num) + '...' 
     ).append('<span>More</span>'); 
    }); 
} 

我有一个文本使用功能切片的内容。jquery点击禁用功能

有一个按钮,当用户点击时,我想禁用功能只有$this .content & output所有文字没有切片。

如何禁用功能?

+2

没有你在哪里保持原单的副本,所以你怎么能撤销吗?现代的CSS有'文本溢出:省略号' – epascarello

+1

文本溢出仅适用于单行 – user2178521

+1

但是,您需要保留原始副本以便稍后恢复原始文件。 – techfoobar

回答

1

制作原件的副本并将其存储在数据属性中。循环并放回文本。

function textoverflow(ele, num){ 
    ele.each(function() { 
     var item = $(this); 
     var orgText = item.text(); 
     if (orgText.length>num) { 
      item.data("orgtext", orgText); 
      item.text(item.text().slice(0,num) + '...'); 
      item.append('<span>More</span>'); 
     } 
    }); 
} 

function undoTextoverflow(ele){ 
    ele.each(function() { 
     var item = $(this); 
     var orgText = item.data("orgtext"); 
     if (orgText) { 
      item.text(orgText); 
     } 
    }); 
} 


$(function(){ 
    textoverflow($('.content'),100); 

    $('.content').on("click", "span", function(){ 
     var parentElem = $(this).closest(".content"); 
     undoTextoverflow(parentElem); 
    }); 
}); 
+0

是可能的使用跨$(此),只有打开文本,因为我有可能文本具有相同的类和共享此功能 – user2178521

+0

我向你展示了如何通过点击跨度来调用它。理想情况下,它将成为主播。 – epascarello

1

您需要保留原件的副本以便稍后恢复。例如:

function truncate(ele, num){ 
    ele.each(function() { 
     var self = $(this); // cache this, since we are using it in 3 places 
     // keep a copy of the original text, before truncating 
     self.data('original', self.text()).text(
      self.text().slice(0,num) + '...' 
     ).append('<span>More</span>'); 
    }); 
} 

function showFull(ele){ 
    ele.each(function() { 
     var self = $(this); 
     // restore orignial and remove the More link 
     self.text(self.data('original').find('span').remove(); 
    }); 
} 

$('document').ready(function(){ 
    truncate($('.content'),100); 

    $('span').click(function(){ 
     showFull($(this).closest('.content')); 
    }); 
}); 
1
$('document').ready(function(){ 

    function bindTriggerOn($ele, num, yourEvent) { 
     var dot = '...', more = '<span>More</span>'; 
     $ele.on(yourEvent, 
      $ele.each(function() { 
       var $me = $(this); 
       $me.text($me.text().slice(0, num) + dot).append(more); 
      }); 
     }); 
     $ele.trigger(yourEvent); 
    }; 
    function offOn($ele, yourEvent) { 
     $ele.off(yourEvent) 
    }; 
    function textoverflow($ele, num, bl){ 
     var myEvent = "myEvent"; 
     if(bl) { 
      bindTriggerOn($ele, num, myEvent); 
     } else { 
      offOn($ele, myEvent); 
     } 
    }; 
    var $content = $('.content'); 
    textoverflow($content, 100); 

    $('span').click(function(){ 
     textoverflow($content, 0, false); 
    }); 
});