0

我在我的 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,但是浏览页面时仍然不显示结果。

4

2 回答 2

1

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

您的代码也可以简化:

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

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

于 2014-02-18T09:43:17.023 回答
0

不正确

<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>

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

于 2014-03-01T03:39:16.620 回答