jQuery .text()方法返回空字符串

时间:2016-12-20 09:56:41

标签: jquery momentjs

HTML code:

<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();
});

但我只有时钟图标,span是空的。我的代码出了什么问题?

3 个答案:

答案 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();
});

答案 1 :(得分:0)

试试这个,这对我来说很好用

&#13;
&#13;
$(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();
});
&#13;
<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>
&#13;
&#13;
&#13;

答案 2 :(得分:-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);
}