admin管理员组文章数量:1326478
I have two columns. People have to select elements from the first column and it will be added to the second column.
I can use innerHTML but I never know how many elements there are going to be. So when I click the link 'MOVE' I would have to know the ID of the element (in the example element1, element2 or element3). Does someone know how I get the parent of the parent div where the javascript triggered?
Like this:
<div id="column1">
<div id="element1">random tekst 1
<div id="right">
<a href="#" onclick="copy()">MOVE</a>
</div>
</div>
<div id="element2">random tekst 2
<div id="right">
<a href="#" onclick="copy()">MOVE</a>
</div>
</div>
<div id="element3">random tekst 3
<div id="right">
<a href="#" onclick="copy()">MOVE</a>
</div>
</div>
</div>
<div id="column2">
</div>
I have two columns. People have to select elements from the first column and it will be added to the second column.
I can use innerHTML but I never know how many elements there are going to be. So when I click the link 'MOVE' I would have to know the ID of the element (in the example element1, element2 or element3). Does someone know how I get the parent of the parent div where the javascript triggered?
Like this:
<div id="column1">
<div id="element1">random tekst 1
<div id="right">
<a href="#" onclick="copy()">MOVE</a>
</div>
</div>
<div id="element2">random tekst 2
<div id="right">
<a href="#" onclick="copy()">MOVE</a>
</div>
</div>
<div id="element3">random tekst 3
<div id="right">
<a href="#" onclick="copy()">MOVE</a>
</div>
</div>
</div>
<div id="column2">
</div>
Share
Improve this question
asked Jan 29, 2010 at 12:42
JeroenVdbJeroenVdb
8081 gold badge13 silver badges22 bronze badges
1 Answer
Reset to default 6You can pass a reference of the anchor element to the function copy and use parentNode
attribute to get the immediate parent.
<a href="#" onclick="copy(this)">MOVE</a>
function copy(elem)
{
alert(elem.parentNode.parentNode)
}
See parentNode
Your plete copy function will be
function copy(elem)
{
var target = document.getElementById ( "column2");
target.appendChild ( elem.parentNode.parentNode );
}
Working Demo
Note
Also there is a problem with your HTML. You have multiple elements with the same id. This is not valid. You have multiple div elements with the same id "right". Change these to unique ids.
本文标签: htmlHowto copy a whole div to another div when javascript is triggered in the divStack Overflow
版权声明:本文标题:html - Howto copy a whole div to another div when javascript is triggered in the div - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742206964a2432994.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论