How to write an element inside a div using jQuery?
How do I write a <div&开发者_开发百科gt;
inside another with jQuery?
I have a <div>
that I can not modify in HTML because I am working in a CMS. So I want to write an element (<div>
) inside this <div>
with a click function.
I already have created the click function, but how do I write with jQuery a <div>
INSIDE another specific <div>
?
You could select the existing div, and append a new div to it:
$('#OuterDiv').append('<div id="innerDiv"></div>');
$('.clickaclick').click( function(){
$('.parent_div').html('<div />');
});
That should do it, assuming you don't care what's in that .parent_div
. There are so many approaches to doing this kind of thing, it'd do you best to read up a bit on jQuery's DOM insertion methods.
http://api.jquery.com/html/
http://api.jquery.com/append/
http://api.jquery.com/appendTo/
http://api.jquery.com/prepend/
http://api.jquery.com/prependTo/
http://api.jquery.com/text/
Like this:
$("#div1").click(function() {
$(this).append("<div>new div</div>");
});
or this:
$("#div1").click(function() {
var $div = $("<div/>")
.attr("id", "div2")
.html("new div");
$(this).append($div);
});
create a new element
$('<div>')
then append that to where you need to
$('<div>').text('im a new div').appendTo($('#divId'));
hope this helps
You can use the jQuery wrap() method, that will wrap you inner div inside another http://api.jquery.com/wrap/
精彩评论