javascript - 我的 javascript 脚本根本不起作用

标签 javascript

我正在用 javascript 编写一种博客发布引擎,但我的代码不起作用!我求求你,请告诉我这段代码有什么问题(我是巴西人,所以注释是葡萄牙语的,但你可以忽略它们:

<!DOCTYPE html>
<html>
<head>
<style>
    .new {
        background-color: #DDDDDD;
          margin-top: 15px;
          margin-left: 30px;
          margin-right: 30px;
          display: none;
          width:600px;
          height:600px;
    }

    #entries{
        width:500px;
        height:500px;
        background-color:blue;
    }



    .newtwo{
        background-color: #DDDDDD;
        margin-top:15px;
        margin-left: 30px;
        margin-right:30px;
    }

    .inputs{
        padding-bottom: 30px;
        width : 500px;
        height:200px;
        overflow:scroll;
    }

    button{
        width:150px;
        height:100px;
        background-color:#3333CC;
        font-family:Impact;
        font-size: 25px;
        color: white;
        border-radius:15px;
        margin-left:30px;
        padding-top:15px;
    }

    </style>
    <script type="text/javascript">
    //crie o objeto Inserir, responsável pelo conteúdo inserido
    function Inserir (title, text, date) {
        //texto principal
        this.text = text;
        //data de publicacao
        this.date = date;
        //título da publicação
        this.title = title;
    }

    var date = new Date();
    var dateFormat = date.getDate() + "/" + (date.getMonth() + 1) + "/" + date.getFullYear();

    //cria o objeto insert
    var insert = new Inserir(document.getElementById("title").value, document.getElementById("body").value, new Date("13/10/2015"));

    document.getElementById("publish").onclick = function(){
        var entryText = document.createElement("p");
        //gere o texto formatado
        entryText.appendChild(document.createTextNode(document.getElementById("title").value));
        document.getElementById("entries").appendChild(entryText);
    }

</script>

</head>
<body>
<div id = "insert" class="new">
<form>
    <input type= "text" id="title" class="inputs" placeholder="Put title here"><br />
    <input type = "text" id="body" size="100" class="inputs" placeholder="write the main text here"><br />

</form>
</div>
<button id="add" onclick="document.getElementById('insert').style.display='block';">Add new entry</button>
<button id="publish" onclick="createEntry();"> Publish! </button>
<div id="entries"></div>
</body>
</html>

最佳答案

尝试将所有脚本放入 window.onload 事件中,如下所示:

window.addEventListener('load', function() {
  //Your javascript code goes here
}, false);

关于javascript - 我的 javascript 脚本根本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33109336/

相关文章:

javascript - React Hooks - 根据状态有条件地渲染组件

javascript - 单击时关闭 Twitter Bootstrap 下拉弹出窗口

javascript - 我如何从引用文档中引用表格数据?

javascript - 从子元素中删除 Size 和 font-size

javascript - slideUp() slideDown() 不能正常工作

javascript - 客户端 HTML 表单验证

javascript - 更改 2 个进度条的过渡和样式

javascript - 单击按钮时更改嵌入式 youtube 视频中的时间

javascript - ChartJS 未更新

javascript - GWT : logic of my application embeded in the ui interface?