我在页面上动态插入 DIV,这是简化的示例:
HTML:
<div class="left-column">
<p>
<label>Company Name:</label>
<input type="text" maxlength="255" id="BodyContentPlaceholder_LeftColumnContentPlaceHolder_CompanyNameTextBox" />
</p>
<p>
<label>Phone:</label>
<input type="text" maxlength="255" id="BodyContentPlaceholder_LeftColumnContentPlaceHolder_PhoneNumberTextBox" __id="105" />
</p>
</div>
<div class="right-column">
<p>
<label __id="109">Contact Name:</label>
<input type="text" maxlength="255" id="BodyContentPlaceholder_LeftColumnContentPlaceHolder_ContactNameTextBox" __id="110" />
</p>
<p>
<label>Email:</label>
<input type="text" maxlength="255" id="BodyContentPlaceholder_LeftColumnContentPlaceHolder_EmailTextBox" /> </p>
</div>
<div style="clear:both;">
<input type="submit" value="Submit Request" id="myButton" />
</div>
CSS:
.inputError {
border: 1px solid #006;
background: pink;
}
.divError {
border: 1px solid black;
background: red;
}
label {
float:left;
width:120px;
text-align:right;
margin-right:5px;
}
input[type="text"] {
width: 150px;
}
.left-column, .right-column {
float:left;
position: relative;
}
.left-column p, .right-column p {
padding-bottom: 10px;
}
.left-column {
margin-right:5px;
}
JS:
$(document).ready(function() {
$("#myButton").on("click", DoTrick);
});
function DoTrick()
{
var $input = $("#BodyContentPlaceholder_LeftColumnContentPlaceHolder_CompanyNameTextBox");
var $div = $("<div/>")
.attr("id", "GGG")
.addClass("divError")
.text("This field has error");
$div.insertAfter($input);
}
但理想情况下,只要我知道输入元素我想添加显示在其右上角的 DIV,我通常希望它能够正常工作,这是应该“浮动”的验证消息
现在,当我添加 DIV 时,它会破坏布局并且不会去我需要的地方。