javascript - 所选 event.target 的父 div 的边框轮廓

标签 javascript jquery html

我想在所选 event.targetparent 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");
$(event.target).parent().css("border", "1px solid red");
    event.preventDefault();
	clicked_link = $(event.target).text();
	clicked_url = $(event.target).attr("href");
	clicked_id = $(event.target).attr("id");
	
	
	
	//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);
});

$(".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($(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;
}
<!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.1.1/jquery.min.js"></script>

    <title>title</title>
  </head>
  <body>
  
  <div>
  Lorem Ipsum..
  
  <a href="http://goo.com" id="id-goo" class="sim-row-edit" data-type="link">right click on me and my parent div should get a border line</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">Get Name</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>

最佳答案

您可以使用

$(event.target).parent().addClass("selected"); 

为父级添加边框。

将此 CSS 添加到样式表中

.selected {
  border: 1px solid red;
}

要删除边框,您可以使用

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

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

相关文章:

javascript - 最小和最大日期

java - 可以在Java中使用C头文件吗?

php - 使用 Javascript 和 PHP 动态添加查询 XML 源的新表和行

javascript - Dropzone JS 不是部分 View 中的函数错误

html - 如何用输入和按钮创建三 Angular 形的形式?

javascript - AngularJS 待办事项列表应用程序 - 在页面刷新时保持列表

javascript - iscroll 在 phonegap 中不起作用?如何在 phonegap 中使用 iscroll

javascript - onclick:改变位置后做一些事情

javascript - 通过从字段中删除引号、支持标题和允许其他分隔符来改进 csv 文件阅读器

Javascript ES6 代码优化