javascript - 克隆所选 event.target 的父 div

标签 javascript jquery html

当我右键单击链接时,我想获取其父 div 的所有代码,并且应该将其克隆到该 div 之后。

我尝试了一些代码,但它不起作用,请参阅下面的代码。我还想删除选定的父 div

下面是我的代码:

// when we're about to show the context menu, show our own instead
$(document).on("contextmenu", function(event) {
  // Avoid the real one if this is the link
  if ($(event.target).hasClass("sim-row-edit")) {
	  console.log("right click identified");
	 
	 // add border
	 $(event.target).parent().addClass("selected");
	 
    event.preventDefault();
	clicked_link = $(event.target).text();
	clicked_url = $(event.target).attr("href");
	clicked_id = $(event.target).attr("id");
	
	//$(event.target).text("ttt");
	
	//alert(clicked_link);
    // Show contextmenu
    $(".custom-menu").show(100).
    css({
      top: event.pageY + "px",
      left: event.pageX + "px"
    });
  }
});

// hide our context menu when the document is clicked
$(document).on("mouseup", function() {
  $(".custom-menu").hide(100);
  $(".selected").removeClass("selected");

});

$(".custom-menu li").click(function() {
  //alert("hii2");
  // This is the triggered action name
  switch ($(this).attr("data-action")) {
    // A case for each action. Should personalize to your actions
    case "first":
     // console.log("first");
	   console.log("-----");
	   console.log(clicked_link);
	   console.log(clicked_url);
	   console.log(clicked_id);
	   
	   
	   //trying to clone the div using below code but it is not working.
	   thisDiv = $(event.target).parent();
	   $(thisDiv).clone().insertAfter(thisDiv);
	
	  //console.log($(this).parent().text());
      break;
    case "second":
      console.log("second");
      break;
    case "third":
      console.log("third");
      break;
  }
});
.custom-menu {
  display: none;
  z-index: 1000;
  position: absolute;
  background-color: #fff;
  border: 1px solid #ddd;
  overflow: hidden;
  width: 120px;
  white-space: nowrap;
  font-family: sans-serif;
  -webkit-box-shadow: 2px 2px 7px 0px rgba(50, 50, 50, 0.5);
  -moz-box-shadow: 2px 2px 7px 0px rgba(50, 50, 50, 0.5);
  box-shadow: 2px 2px 7px 0px rgba(50, 50, 50, 0.5);
}

.custom-menu li {
  padding: 5px 10px;
}

.custom-menu li:hover {
  background-color: #4679BD;
  cursor: pointer;
}

.selected {
  border: 1px solid red;
}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
	<link href="style.css" rel="stylesheet" type="text/css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

    <title>title</title>
  </head>
  <body>
  
  <div>
  Lorem..
  <a href="http://goo.com" id="id-goo" class="sim-row-edit" data-type="link">right click on me and click clone, but its not working</a>
  </div>
   
   
   <div class="sim-row-edit">
   <a href="http://voo.com" id="id-voo"  data-type="link">voo</a>
   </div>
   
<ul class='custom-menu'>
  <li data-action = "first">Clone</li>
  <li data-action = "second">Second thing</li>
  <li data-action = "third">Third thing</li>
</ul>

<script type="text/javascript" src="script.js"></script>
  </body>
</html>

最佳答案

在您的代码示例中,在事件处理程序中 $(".custom-menu li").click= function() { ... } event.target指向 li 元素并获取其 parent 会得到 ul 元素,该元素会被克隆并插入到 ul 中,给出一个嵌套列表,即上下文菜单内的上下文菜单。

要解决此问题,请在右键单击时获取所单击元素的引用以及其他属性,并在该事件处理程序中引用以进行克隆。请参阅代码以进行理解。

// when we're about to show the context menu, show our own instead
$(document).ready(function() {
  console.log('Document ready');
});

$(document).on("contextmenu", function(event) {
  // Avoid the real one if this is the link
  if ($(event.target).hasClass("sim-row-edit")) {
    console.log("right click identified");

    // add border
    $(event.target).parent().addClass("selected");

    event.preventDefault();
    target_element = $(event.target);
    clicked_link = $(event.target).text();
    clicked_url = $(event.target).attr("href");
    clicked_id = $(event.target).attr("id");

    //$(event.target).text("ttt");

    //alert(clicked_link);
    // Show contextmenu
    $(".custom-menu").show(100).
    css({
      top: event.pageY + "px",
      left: event.pageX + "px"
    });
  }
});

// hide our context menu when the document is clicked
$(document).on("mouseup", function() {
  $(".custom-menu").hide(100);
  $(".selected").removeClass("selected");

});

$(".custom-menu li").click(function() {
  //alert("hii2");
  // This is the triggered action name
  switch ($(this).attr("data-action")) {
    // A case for each action. Should personalize to your actions
    case "first":
      // console.log("first");
      console.log("-----");
      console.log(clicked_link);
      console.log(clicked_url);
      console.log(clicked_id);
      console.log(target_element);


      //trying to clone the div using below code but it is not working.
      console.log(event.target);
      thisDiv = target_element.parent();
      thisDiv.clone().insertAfter(thisDiv);

      //console.log($(this).parent().text());
      break;
    case "second":
      console.log("second");
      break;
    case "third":
      console.log("third");
      break;
  }
});
.custom-menu {
  display: none;
  z-index: 1000;
  position: absolute;
  background-color: #fff;
  border: 1px solid #ddd;
  overflow: hidden;
  width: 120px;
  white-space: nowrap;
  font-family: sans-serif;
  -webkit-box-shadow: 2px 2px 7px 0px rgba(50, 50, 50, 0.5);
  -moz-box-shadow: 2px 2px 7px 0px rgba(50, 50, 50, 0.5);
  box-shadow: 2px 2px 7px 0px rgba(50, 50, 50, 0.5);
}

.custom-menu li {
  padding: 5px 10px;
}

.custom-menu li:hover {
  background-color: #4679BD;
  cursor: pointer;
}

.selected {
  border: 1px solid red;
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <link href="style.css" rel="stylesheet" type="text/css" />
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

  <title>title</title>
</head>

<body>

  <div>
    Lorem..
    <a href="http://goo.com" id="id-goo" class="sim-row-edit" data-type="link">right click on me and click clone, but its not working</a>
  </div>


  <div>
    <a href="http://voo.com" id="id-voo" data-type="link">voo</a>
  </div>

  <ul class='custom-menu'>
    <li data-action="first">Clone</li>
    <li data-action="second">Second thing</li>
    <li data-action="third">Third thing</li>
  </ul>

  <script type="text/javascript" src="script.js"></script>
</body>

</html>

关于javascript - 克隆所选 event.target 的父 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52204790/

相关文章:

javascript - 调用传入 Angular 指令的函数返回该函数而不是调用它

javascript - Angular:添加额外的 js 时未捕获语法错误

javascript - 使用 anchor 标签平滑滚动时左右轮播控件不起作用

jquery - 切换开关状态链接图标

javascript - 从 JavaScript 构建 HTML 的最快方法是什么?

javascript - "Null"HTML DOM 元素?

javascript - Fabric.js - 更改矩形填充

javascript - 如何在主干或 ember 或任何 javascript MVC 框架中编写简单 jQuery AJAX 调用的等效项,以便我可以对其进行单元测试

javascript - var ball = document.getElementById ("ball");返回空对象?

javascript - 单击时使用 Jquery 上下动画 div