如何div
在jQuery中创建元素?
在jQuery中创建div元素
How about this? Here, pElement
refers to the element you want this div
inside (to be a child of! :).
$("pElement").append("<div></div");
You can easily add anything more to that div
in the string - Attributes, Content, you name it. Do note, for attribute values, you need to use the right quotation marks.
I hope that helps code. :) (I use)
function generateParameterForm(fieldName, promptText, valueType) {
//<div class="form-group">
//<label for="yyy" class="control-label">XXX</label>
//<input type="text" class="form-control" id="yyy" name="yyy"/>
//</div>
// Add new div tag
var form = $("<div/>").addClass("form-group");
// Add label for prompt text
var label = $("<label/>").attr("for", fieldName).addClass("control-label").text(promptText);
// Add text field
var input = $("<input/>").attr("type", "text").addClass("form-control").addClass(valueType).attr("id", fieldName).attr("name", fieldName);
// lbl and inp => form
$(form).append(label).append(input);
return $(form);
}
I think this is the best way to add a div:
To append a test div to the div element with ID div_id:
$("#div_id").append("div name along with id will come here, for example, test");
Now append HTML to this added test div:
$("#test").append("Your HTML");
If it is just an empty div, this is sufficient:
$("#foo").append("<div>")
or
$("#foo").append("<div/>")
It gives the same result.
document.createElement('div');
alternatively to append()
you can also use appendTo()
which has a different syntax:
$("#foo").append("<div>hello world</div>");
$("<div>hello world</div>").appendTo("#foo");
All these worked for me,
HTML part:
<div id="targetDIV" style="border: 1px solid Red">
This text is surrounded by a DIV tag whose id is "targetDIV".
</div>
JavaScript code:
//Way 1: appendTo()
<script type="text/javascript">
$("<div>hello stackoverflow users</div>").appendTo("#targetDIV"); //appendTo: Append at inside bottom
</script>
//Way 2: prependTo()
<script type="text/javascript">
$("<div>Hello, Stack Overflow users</div>").prependTo("#targetDIV"); //prependTo: Append at inside top
</script>
//Way 3: html()
<script type="text/javascript">
$("#targetDIV").html("<div>Hello, Stack Overflow users</div>"); //.html(): Clean HTML inside and append
</script>
//Way 4: append()
<script type="text/javascript">
$("#targetDIV").append("<div>Hello, Stack Overflow users</div>"); //Same as appendTo
</script>
A short way of creating div is
var customDiv = $("<div/>");
Now the custom div can be appended to any other div.
d = document.createElement('div');
$(d).addClass(classname)
.html(text)
.appendTo($("#myDiv")) //main div
.click(function () {
$(this).remove();
})
.hide()
.slideToggle(300)
.delay(2500)
.slideToggle(300)
.queue(function () {
$(this).remove();
});
div = $("<div>").html("Loading......");
$("body").prepend(div);
$("<div/>").appendTo("div#main");
will append a blank div to <div id="main"></div>
You can use
.add()
to create a new jQuery object and add to the targeted element. Use chaining then to proceed further.For eg jQueryApi: