在JavaScript中写入div内的文本?

use*_*565 2 html javascript replace button

我有一个document.write()用于写入页面的JavaScript函数.我的问题是,当我单击按钮调用该函数时,document.write()将我已经拥有的内容替换为我正在编写的内容.有没有办法从JavaScript写一个特定的div文本?

这是我的HTML代码:

<html>
<head>
    <link href="style.css" rel="stylesheet" type="text/css">
    <script type="text/javascript" src="javascript.js">
    </script>
    <script>
        // Calling the Google Maps API
    </script>

    <script>
        <!-- JavaScript to load Google Maps -->
    </script>
</head>

<body>
    <div class="content">
        <div id="googleMap"></div>
        <div id="right_pane_results">hi</div>this -->
        <div id="bottom_pane_options">
            <button onclick="todaydate();">Try It</button>
        </div>
    </div>

</body>
Run Code Online (Sandbox Code Playgroud)

...和我的JavaScript代码(我从互联网上获得的东西只是为了测试):

function todaydate() {
    var today_date = new Date();
    var myyear = today_date.getYear();
    var mymonth = today_date.getMonth() + 1;
    var mytoday = today_date.getDate();

    document.write("<h1>" + myyear + "/" + mymonth + "/"+mytoday + "/h1">);
}
Run Code Online (Sandbox Code Playgroud)

我希望文本在按钮下方.任何帮助,将不胜感激.

谢谢,乔希

Edg*_*ado 10

你应该避免document.write.你最好把结果放到另一个div:

 <div id="bottom_pane_options">
     <button onclick="todaydate();">Try It</button>
     <div id="results"></div>   <!-- Added div ! -->
 </div>
Run Code Online (Sandbox Code Playgroud)

然后

function todaydate() {
    var today_date=new Date();
    var myyear=today_date.getYear();
    var mymonth=today_date.getMonth() + 1;
    var mytoday=today_date.getDate();

    document.getElementById('results').innerHTML ="<h1>" + myyear + "/" + mymonth + "/" + mytoday + "</h1>";
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我们将结果写入resultsdiv .innerHTML.

希望这可以帮助.干杯