我用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>
答案 0 :(得分:1)
尝试将所有脚本放在window.onload
这样的事件中:
window.addEventListener('load', function() {
//Your javascript code goes here
}, false);