Javascript代码无法在Firefox上运行

时间:2015-12-18 05:17:45

标签: javascript jquery toggle

我有这段代码将文本截断为多个字符,然后在您单击“更多”时显示,并在单击“更少”时切换回来。 这是我的代码:

<script type="text/javascript">
 //<![CDATA[
var $j = jQuery.noConflict();
//]]>
$j(window).load(function() {
    var showChar = 600;  // The default length of characters to be shown
    var ellipsestext = "...";
    var moretext = "More >>";
    var lesstext = "Less <<";       

    $j('.more').each(function() {
        var content = $j(this).html();   
        if(content.length > showChar) {  
            var c = content.substr(0, showChar);
            var h = content.substr(showChar, content.length - showChar);     
            var html = c + '<span class="moreellipses">' + ellipsestext+ '&nbsp;</span><span class="morecontent"><span>' + h + '</span>&nbsp;<a href="" class="morelink">' + moretext + '</a></span>';   
            $j(this).html(html);
        }    
    });

    $j(".morelink").click(function() {
        if($j(this).hasClass("less")) {
            $j(this).removeClass("less");
            $j(this).html(moretext);
        } else {
            $j(this).addClass("less");
            $j(this).html(lesstext);
        }
        $j(this).parent().prev().toggle();
        $j(this).prev().toggle();
        return false;
    });
});
</script>

它适用于Chrome和Opera,但不适用于Firefox。我的代码有问题吗?

2 个答案:

答案 0 :(得分:0)

尝试:

$j(".more").on('click','.morelink',function(){
        if($j(this).hasClass("less")) {
            $j(this).removeClass("less");
            $j(this).html(moretext);
        } else {
            $j(this).addClass("less");
            $j(this).html(lesstext);
        }
        $j(this).parent().prev().toggle();
        $j(this).prev().toggle();
        return false;
    });

&安培;使用文档就绪语句($(function(){})),.load()是一个ajax函数

答案 1 :(得分:0)

这个演示很完美。

var $j = jQuery.noConflict();
$j(window).load(function() {
  var showChar = 600; // The default length of characters to be shown
  var ellipsestext = "...";
  var moretext = "More >>";
  var lesstext = "Less <<";

  $j('.more').each(function() {
    var content = $j(this).html();
    if (content.length > showChar) {
      var c = content.substr(0, showChar);
      var h = content.substr(showChar, content.length - showChar);
      var html = c + '<span class="moreellipses">' + ellipsestext + '&nbsp;</span><span class="morecontent"><span>' + h + '</span>&nbsp;<a href="" class="morelink">' + moretext + '</a></span>';
      $j(this).html(html);
    }
  });

  $j(".morelink").click(function() {
    if ($j(this).hasClass("less")) {
      $j(this).removeClass("less");
      $j(this).html(moretext);
    } else {
      $j(this).addClass("less");
      $j(this).html(lesstext);
    }
    $j(this).parent().prev().toggle();
    $j(this).prev().toggle();
    return false;
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<span class="more">sdgagafgadfgafdgafdgafgadfghshdghdghdfhdfjshlkjabgaglakjgfaljgLJGNlkgfbdfagnfldkgalkjfgalkgjaflkghalkfgjhakldfgsfgj;adsfkgjs;dfklj;dlgkjsdfgkjsdf;gkjsfd;kgsjh'lkjglhkd'sfhkld'hklds'ghkl';glkh'sdlgkh'slgkh'slkhs'lhks'hkls'lkhs'klhs'hkl'shklfjghlakghalfjghalkgjf;bvklb;kjaajgpijg;aflkdjg;akflgj;akfgj;akfljg;akfgja;lfkgja;lfkgadfadfafadfafaafe;gjegjlfkgjs;k;kgsl;kfdjg;skdfjg;lskjfg;lksfjg;slkfjg;slfdgsf;kgjwoirwprlkfgslkrglkhsfdlkgjhglkruhtsgfkjlhakjdshgjkhasfguhreihkghlzkjfdghkugeirgeghdkjhgskjdfgksrhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgkgjwoirwprlkfgslkrglkhsfdlkgjhglkruhtsgfkjlhakjdshgjkhasfguhreihkghlzkjfdghkugeirgeghdkjhgskjdfgksrhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgkgjwoirwprlkfgslkrglkhsfdlkgjhglkruhtsgfkjlhakjdshgjkhasfguhreihkghlzkjfdghkugeirgeghdkjhgskjdfgksrhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgkgjwoirwprlkfgslkrglkhsfdlkgjhglkruhtsgfkjlhakjdshgjkhasfguhreihkghlzkjfdghkugeirgeghdkjhgskjdfgksrhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgkgjwoirwprlkfgslkrglkhsfdlkgjhglkruhtsgfkjlhakjdshgjkhasfguhreihkghlzkjfdghkugeirgeghdkjhgskjdfgksrhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgkgjwoirwprlkfgslkrglkhsfdlkgjhglkruhtsgfkjlhakjdshgjkhasfguhreihkghlzkjfdghkugeirgeghdkjhgskjdfgksrhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgkgjwoirwprlkfgslkrglkhsfdlkgjhglkruhtsgfkjlhakjdshgjkhasfguhreihkghlzkjfdghkugeirgeghdkjhgskjdfgksrhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhgushfogisuhfdgisuhfgishgiushgiushgiushgisuhgiureheihiegsfjghdkhgisurhg</span>