2014-02-17 81 views
0

我在SharePoint Online帳戶中設置了腳本編輯器Web部件。結果顯示當我編輯頁面,但不是當瀏覽頁面。這個jQuery,Moment.js腳本不會在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,但是結果還是瀏覽網頁時不顯示。

+0

嘗試把_spBodyOnLoadFunctionNames.push( 「setMoments」) ;在腳本標籤的頂部。 –

+0

@ Yevgeniy.Chernobrivets沒有運氣。相同的結果。 – Mark

回答

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> 

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

+0

奇怪....也許是因爲您在頁面中使用的DOCTYPE! :-) – AymKdn

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

+0

我更新了使用你的腳本和相同的行爲。結果顯示在我編輯頁面時,但在瀏覽頁面時不顯示。感謝您的簡化代碼。 – Mark