I am trying to learn how to clone an element using classname and append it to the body. here is what i have done but i am not getting any output. is there anything wrong ?
HTML:
<div class="check">hello</div>
CSS:
.check {
top: 100px;
}
JavaScript:
var elem = document.getElementsByClassName('.check');
var temp = elem[0].clonenode(true);
document.body.append(temp);
JSFiddle Link:
/
if not JS, jquery solution is also weled.
I am trying to learn how to clone an element using classname and append it to the body. here is what i have done but i am not getting any output. is there anything wrong ?
HTML:
<div class="check">hello</div>
CSS:
.check {
top: 100px;
}
JavaScript:
var elem = document.getElementsByClassName('.check');
var temp = elem[0].clonenode(true);
document.body.append(temp);
JSFiddle Link:
http://jsfiddle/hAw53/378/
if not JS, jquery solution is also weled.
Share Improve this question asked Sep 28, 2014 at 14:15 S JS J 3,2201 gold badge24 silver badges32 bronze badges5 Answers
Reset to default 4You were almost there:
var elem = document.getElementsByClassName('check'); // remove the dot from the class name
var temp = elem[0].cloneNode(true); // capitalise "Node"
document.body.appendChild(temp); // change "append" to "appendChild"
<div class="check">hello</div>
You have 3 errors. Correct code:
var elem = document.getElementsByClassName('check'); // check, not .check
var temp = elem[0].cloneNode(true); // cloneNode, not clonenode
document.body.appendChild(temp); // appendChild, not append
Demo: http://jsfiddle/hAw53/379/
There are a few issues with your code.
getElementsByClassName()
takes a class name (check
), not a selector (.check
)cloneNode()
is spelled with a capital N (notclonenode()
)appendChild()
is the name of the DOM method for appending a child (notappend()
)
Correct version:
var elem = document.getElementsByClassName('check');
var temp = elem[0].cloneNode(true);
document.body.appendChild(temp);
You can do:
$('.check').clone().appendTo('body');
You're code had errors. First you used class selector and not the class name. Then you used an undefined property(properties are case sensitive) and you've to use appendChild
instead of append
which is a part of jQuery. You're too much confused with native javascript and jQuery.
in Jquery it's very simple, you just need to define inside what the new element apears.
var elem = $('.check');
elem.clone().prependTo( "body");
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744205758a4563094.html
评论列表(0条)