2015-10-13 58 views
0

我正在用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> 
+1

您的DOM是沒有準備在這一點上還有沒有'createEntry'功能 – MinusFour

+1

創建一個的jsfiddle,所有的部分分離出來:。http://jsfiddle.net/hgzd4b78/而這裏的一個它只是一個文件:http://jsfiddle.net/q8sac8rn/ – CBredlow

+0

請詳細描述。「我的代碼不起作用!」幾乎沒有錯誤信息那麼有用,o描述發生了什麼。請參閱http://stackoverflow.com/help/mcve。 – Prune

回答

1

儘量把所有的腳本在window.onload事件這樣:

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