簡體   English   中英

此jQuery Moment.js腳本將不會顯示在SharePoint Online腳本編輯器Web部件中,為什么有任何想法?

[英]This jQuery, Moment.js script won't display in SharePoint Online script editor web part, any ideas why?

我在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,但是在瀏覽頁面時仍然不顯示結果。

您是否嘗試調試,例如在setMoments() alert()之前添加alert() setMoments()以確保在某些時候到達了代碼的這一部分?

您的代碼也可以簡化:

<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 $(documen).ready_spBodyOnLoadFunctionNames

不正確

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

顯然,您必須指定語言:-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM