【问题标题】:This jQuery, Moment.js script won't display in SharePoint Online script editor web part, any ideas why?此 jQuery、Moment.js 脚本不会显示在 SharePoint Online 脚本编辑器 Web 部件中,有什么想法吗?
【发布时间】:2014-03-16 06:26:42
【问题描述】:

我在我的 SharePoint Online 帐户中设置了脚本编辑器 Web 部件。结果会在我编辑页面时显示,而不是在浏览页面时显示。

这是我正在使用的脚本:

<h3>You got married: <span id="married"></span></h3>
<h3>Adam was born: <span id="adamBorn"></span></h3>   

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="https://mysite.sharepoint.com/tech/Scripts/moment-with-langs.js">    </script>
<script>

  function setMoments(){
        var married = $('#married'),
            date = moment("1951-12-05", "YYYY-MM-DD"),
            update = function(){
                   married.html(date.fromNow());
            };
       update();

    var adamBorn = $('#adamBorn'),
        date = moment("1992-06-14", "YYYY-MM-DD"),
        update = function(){
                   adamBorn.html(date.fromNow());
                 };


    update();

  };

  _spBodyOnLoadFunctionNames.push("setMoments");
</script>

我也尝试了使用document.ready,但是浏览页面时仍然不显示结果。

【问题讨论】:

  • 尝试把_spBodyOnLoadFunctionNames.push("setMoments");在脚本标签的顶部。
  • @Yevgeniy.Chernobrivets 运气不好。结果相同。

标签: javascript jquery sharepoint momentjs sharepoint-online


【解决方案1】:

您是否尝试过调试,例如在setMoments() 之前添加alert() 以确保在某个时候到达这部分代码?

您的代码也可以简化:

<script>
$('#married').html(moment("1951-12-05", "YYYY-MM-DD").fromNow());
$('#adamBorn').html(moment("1992-06-14", "YYYY-MM-DD").fromNow());
</script>

因为您的脚本在这两个字段之后,所以不需要使用$(documen).ready_spBodyOnLoadFunctionNames

【讨论】:

  • 我更新为使用您的脚本和相同的行为。结果在我编辑页面时显示,但在浏览页面时不显示。感谢您提供简化的代码。
【解决方案2】:

不正确

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="https://mysite.sharepoint.com/tech/Scripts/moment-with-langs.js">    </script>

正确

<script language="javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script language="javascript" src="https://mysite.sharepoint.com/tech/Scripts/moment-with-langs.js">    </script>

显然你必须指定语言:-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-14
    • 2020-08-15
    • 1970-01-01
    • 2022-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    相关资源
    最近更新 更多