检查给定的div是否在父div中

时间:2017-02-11 06:50:57

标签: jquery html css

我想检查父div中的给定标记'section'。如果'section'标记不存在,则将子div包含在'section'标记中。

jQuery(document).on('keydown', function(e) {
  if (e.keyCode === 34) {
    alert('hello');
    jQuery(".child").wrap("<section class='panell'>Hello</section>");
  }
});
.panell { border:1px solid black }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent" style="background-color:red;min-height:100vh;">
  <div class="child">
   World

  </div>
</div>

我想在类'parent'中检查section标签是否存在,并在section标签中追加子div包含

1 个答案:

答案 0 :(得分:0)

仅在父元素的直接子元素的情况下包装子元素。

jQuery(document).on('keydown', function(e) {
  if (e.keyCode === 34) {
    alert('hello');
    jQuery(".parent>.child").wrap("<section class='panell'>Hello</section>");
  }
});
  .panell { border:5px solid black } 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent" style="background-color:red;min-height:100vh;">
  <div class="child">


  </div>
</div>