开发者

Javascript string construction by concatenation?

开发者 https://www.devze.com 2023-04-13 01:15 出处:网络
Exploring Javascript (and coming from the Java world). I have the follow开发者_Go百科ing line of code in a script:

Exploring Javascript (and coming from the Java world). I have the follow开发者_Go百科ing line of code in a script:

if (jQuery) {  
    document.getElementById("BOTTOM_MID").innerHTML
        = "JQuery Loaded - " + getTime();
}

but it does not work. BOTTOM_MID is not initialized. Yet, the following works:

if (jQuery) {  
    document.getElementById("BOTTOM_MID").innerHTML
        = "JQuery Loaded";
}

Doesn't Javascript understand string construction by concatenation? If yes, how should I proceed?


getTime() is a method of the Date object. Try this:

if (jQuery) {  
    document.getElementById("BOTTOM_MID").innerHTML
        = "JQuery Loaded - " +  new Date().getTime();
}

Since you're using jQuery, I also suggest that you use jQuery's ready() handler and selectors:

$(document).ready(function() {
    $("#BOTTOM_MID").html("JQuery Loaded - " +  new Date().getTime());
});

Here's a working fiddle.


Your code looks fine, try to run this on your chrome developer tools or firebug's console, in this web page:

 document.getElementById("notify-container").innerHTML = "JQuery Loaded - " + "hello"

It will work, indeed.

Perhaps you don't have getTime() defined? It's not a standard javascript function BTW

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号