Need to clone the selected <div> - angular 2

二次信任 提交于 2019-12-12 04:48:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!