使用DOM添加元素document.write

fli*_*dia 4 javascript xhtml dom document.write

我刚学会(不用谢谢IE)我不能在XHTML中使用document.write脚本.但是,通过使用DOM添加元素似乎有办法解决它.我不知道.这对我来说很陌生.

这是JS:

copyright=new Date();
update=copyright.getFullYear();
document.write("Copyright © 2004-"+ update + " flip-flop media");
Run Code Online (Sandbox Code Playgroud)

那么,有没有办法在XHTML中使用这个脚本?IE 8显示一个带有错误警告的空白区域.IE 7只显示警告.FF确实正确显示它.

它显示没有错误,并在我的一个页面上正确显示:http: //clients.flipflopmedia.com

但不在主页上:http: //www.flipflopmedia.com/NewSite/index.html

Dav*_*och 6

要回答您的问题:附加版权声明作为页面上的最后一个元素.

(function(){
    // create almost any element this way...
    var el = document.createElement("div");
    // add some text to the element...
    el.innerHTML = "Copyright © 2004-"+ (new Date).getFullYear() + " flip-flop media";
    // "document.body" can be any element on the page.
    document.body.appendChild(el);
}());
Run Code Online (Sandbox Code Playgroud)

您可以随时将"document.body"更改为您要使用的任何元素.如document.getElementById("copyright").appendChild(el);

jQuery的

你已经在页面上有jQuery.所以只需使用:

$(function(){
    // "body" is a css selector.
    // you can use almost any css selector 
    // to find the element you need. e.g. $("#copyright")...
    $("body").append("Copyright © 2004-"+ (new Date).getFullYear() + " flip-flop media");
    // you can also use html() to replace the existing content.
    // $("#copyright").html("content goes here");
});
Run Code Online (Sandbox Code Playgroud)

你应该做什么:

使用服务器端技术,如php,.net等.

您可能正在服务器上运行php,这意味着以下内容应该适用于您的页面中的任何位置(如果它有一个php扩展名(index.php而不是index.html),当然):

<?php
    echo 'copyright &copy; 2004-' + date("Y") . ' flip-flop media';
?>
Run Code Online (Sandbox Code Playgroud)