2016-12-20 25 views
0

HTML代碼:的jQuery的.text()方法返回空字符串

<div class="m-t-md"> 
    <small class="pull-left"> 
     <i class="fa fa-clock-o"></i> 
     <span id="updatedTime_SalesTab"></span> 
    </small> 
</div> 

JS代碼:

$(document).ready(function() { 
    function updatedDate() { 
     var testDateUtc = moment.utc(); 
     var local = moment(testDateUtc).local(); 

     var updatedTime = moment(local).format("MM/DD/YYYY h:mm A"); 
     console.log('updated: ', updatedTime); // log: "updated: 12/20/2016 11:44 AM" 

     $('#updatedTime_SalesTab').text(updatedTime); 
    } 

    updatedDate(); 
}); 

但我只有時鐘圖標,跨度爲空。我的代碼有什麼問題?

+0

代碼對我來說看起來不錯..你有沒有檢查你的控制檯窗口的錯誤? –

+2

這裏是jsfiddle,工作正常:https://jsfiddle.net/FLhpq/7410/ –

+0

你忘了包含插件? –

回答

0

只需使用HTML

$(document).ready(function() { 
    function updatedDate() { 
     var testDateUtc = moment.utc(); 
     var local = moment(testDateUtc).local(); 

     var updatedTime = moment(local).format("MM/DD/YYYY h:mm A"); 
     console.log('updated: ', updatedTime); // log: "updated: 12/20/2016 11:44 AM" 

     $('#updatedTime_SalesTab').html(updatedTime); 
    } 

    updatedDate(); 
}); 
+0

謝謝你的回答!我試過了,但它不工作(但應該) 。 – DenysM

-1

替換文本在我的情況下,這解決了我的問題:

$(document).ready(function() { 
    updatedDate(); 
}); 

function updatedDate() { 
    var testDateUtc = moment.utc(); 
    var local = moment(testDateUtc).local(); 

    var updatedTime = moment(local).format("MM/DD/YYYY h:mm A"); 
    console.log('updated: ', updatedTime); // log: "updated: 12/20/2016 11:44 AM" 

    $('#updatedTime_SalesTab').text(updatedTime); 
} 
0

試試這個,這個工作正常,我

$(document).ready(function() { 
 
    
 
function updatedDate() { 
 
     var testDateUtc = moment.utc(); 
 
     var local = moment(testDateUtc).local(); 
 

 
     var updatedTime = moment(local).format("MM/DD/YYYY h:mm A"); 
 
     console.log('updated: ', updatedTime); // log: "updated: 12/20/2016 11:44 AM" 
 

 
     $('#updatedTime_SalesTab').text(updatedTime); 
 
    } 
 
    updatedDate(); 
 
});
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.5.1/moment.min.js"></script> 
 
<script> 
 

 

 
function updatedDate() { 
 
    var testDateUtc = moment.utc(); 
 
    var local = moment(testDateUtc).local(); 
 

 
    var updatedTime = moment(local).format("MM/DD/YYYY h:mm A"); 
 
    console.log('updated: ', updatedTime); // log: "updated: 12/20/2016 11:44 AM" 
 

 
    $('#updatedTime_SalesTab').text(updatedTime); 
 
    updatedDate(); 
 
} 
 
</script> 
 
<div class="m-t-md"> 
 
    <small class="pull-left"> 
 
     <i class="fa fa-clock-o"></i> 
 
     <span id="updatedTime_SalesTab"></span> 
 
    </small> 
 
</div>