【发布时间】:2014-10-05 07:28:20
【问题描述】:
我正在使用 javascript 在 div 中插入段落。它的作用是在您单击按钮并将其作为段落附加到特定 div 时获取输入表单的值。问题是它们的边距太大,我不知道如何减少它们(使它们更接近)。在这种情况下我该怎么办?
jsfiddle - 无论如何,我的代码如下所示:
HTML
<div id="red">
<button id="bt_1" onclick="addParagraph()">Add</button>
<form>
<input id="box_top" type="text" placeholder="Spacing is too big -->">
</form>
</div>
<div id="blue">
<!--Paragraphs that are being added here have their top and bottom margin too big, but I don't know how to fix it.-->
<!--Type something in thet input form and keep clicking the add button to see what I mean.-->
</div>
CSS
#blue {
height:100px;
width:250px;
margin-top: 10px;
float:left;
position:relative;
background-color:blue;
overflow:auto;
}
#red {
height:100px;
width:250px;
margin-top: 10px;
float:left;
position:relative;
background-color:red
}
form {
font-size: 12px;
font-family: Verdana, Arial, Sans-Serif;
display: inline-block;
}
#bt_1 {
margin-left:5px;
height:35px;
width:70px;
margin-top: 25px;
border-radius:5px;
}
JavaScript
function addParagraph() {
var word = document.getElementById("box_top").value;
var pMaker = document.createElement("p");
var nodeMaker = document.createTextNode(word);
pMaker.appendChild(nodeMaker);
var blueDiv = document.getElementById("blue");
blueDiv.appendChild(pMaker);
}
【问题讨论】:
-
@SureshPonnukalai 谢谢!我不相信我忽略了这么基本的东西,这让我有点头疼哈哈。感谢您清理它。 :)
-
感谢大家的回复,对我帮助很大。
标签: javascript html css margin appendchild