首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >代码在线运行正常,但在本地文件上运行失败

代码在线运行正常,但在本地文件上运行失败
EN

Stack Overflow用户
提问于 2012-10-22 21:50:16
回答 1查看 136关注 0票数 0

请看下面的代码:CODE

此代码适用于jsfiddle.net,但不适用于本地js代码,此代码显示和隐藏的文本更少

代码语言:javascript
运行
复制
<script type="text/javascript" src="jquery-1.8.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
      var maxheight=118;
      var showText = "More";
      var hideText = "Less";

      $('.textContainer_Truncate').each(function () {
        var text = $(this);
        if (text.height() > maxheight){
            text.css({ 'overflow': 'hidden','height': maxheight + 'px' });

            var link = $('<a href="#">' + showText + '</a>');
            var linkDiv = $('<div></div>');
            linkDiv.append(link);
            $(this).after(linkDiv);

            link.click(function (event) {
              event.preventDefault();
              if (text.height() > maxheight) {
                  $(this).html(showText);
                  text.css('height', maxheight + 'px');
              } else {
                  $(this).html(hideText);
                  text.css('height', 'auto');
              }
            });
        }       
      });
   });​
</script>

htm代码:

代码语言:javascript
运行
复制
<div class="textContainer_Truncate">
      <p>content<br>
        hello<br>
        hello<br>
        hello<br>
          hello<br>
          hello<br>
          hello<br>
          hello<br>
          hello<br>
          hello<br>
          hello<br>
          hello<br>
          hello<br>
          hello<br>
          hello<br>
          hello<br>
          hello<br>
        </p>
    </div>

文件夹内容: index.htm和jquery-1.8.2.min.js如何在本地使用此代码?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-22 22:00:39

我试着复制你所做的,发现它工作得很好,没有错误。

考虑下载此解决方案(一个文件夹包含一个index.html页面以及jQuery1.8.2.js文件)。也许你做了一些额外的Test Project

更新:在复制你的代码时,我注意到一些东西,你的代码中有一个额外的问号,看看下面的图片

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13012869

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档