我将此代码用于“查看更多”功能

http://pastebin.com/SCfneG96

并执行类似的代码

<p class="trunc">Lorem <a href="test.html">ipsum</a>
  dolor sit amet, consectetur adipisicing elit.
  Hic, similique obcaecati quam aspernatur.
</p>
<script>
  $(".trunc").trunc(10);
</script>


但是“ ipsum”不可点击,问题出在哪里?

最佳答案

 (function($) {
    $.fn.trunc = function(numWords) {
        this.each(function() {
            var me = $(this);
            var original = me.html();
            var truncated = original.split(" ");
            if (truncated.length <= numWords) {
                return;
            }
            while (truncated.length > numWords) {
                truncated.pop();
            }
            truncated = truncated.join(" ");
            collapse();

            function expand() {
                me.html(original);
                return false;
            }

            function collapse() {
                me.html(truncated + "... ");
                var link = $('<a href="#" id="seemore">See more</a>');
                link.click(expand);
                me.append().append(link).append();
                return false;
            }
        });
    };
     $(".trunc").trunc(10);
        })(jQuery);


小提琴-http://jsfiddle.net/qdhe5/

10-02 17:19