问题
I need to clone a div which when right clicked will show a clone button.
I am using this code in editpage.ts to open a context menu with clone button
detectRightMouseClick($event) {
// disabling the default browser window which comes on right click
document.addEventListener('contextmenu', event => event.preventDefault());
if($event.which === 3) {
this.rightPanelStyle = {'display':'block','left':$event.clientX + 'px','top':$event.clientY + 'px'};
return false;
}
}
//for clone
clone(){
console.log("Clone function here");
}
Now how can i clone that <div> on which i have right clicked. My editpage.html code is below:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Full screen sections with CSS</title>
<link rel="stylesheet" href="./assets/files/css/style.css">
{{template_skeleton_top_param}}
</head>
<body>
<div #child>
<div>
<p>Clone me</p>
</div>
</div>
</body>
</html>
I want to right click on the div and clone that <div> which is having <p> tag along with the div
回答1:
document.addEventListener("contextmenu", (e) => {
e.preventDefault();
console.log(e, e.srcElement.outerHTML);
this.htmlstring = e.srcElement.outerHTML;
});
来源:https://stackoverflow.com/questions/44581556/need-to-clone-the-selected-div-angular-2