简体   繁体   English

在没有子元素的情况下隐藏或删除具有相同类的元素

[英]Hide or remove elments with same class inside from no child elements

I need hide or remove, divs with same class, but one to one, actually i can delete all divs with same class in one time, and i don´t want this 我需要隐藏或删除具有相同类的div,但是一对一,实际上我可以一次删除所有具有相同类的div,而我不希望这样

The problem it´s the elements are separated not inside in container, and i think that´s the problem, but i need this sctructure, i put my code 问题是元素不是在容器中分开的,我认为这是问题,但是我需要这种结构, 我将代码放入

JQUERY JQUERY

<script>
function close_tabs(idc)
{
$(idc).parent().hide();
}
</script>

CSS STYLE CSS样式

<style>
#elements
{
position:relative;
width:400px;
height:40px;
margin-left:auto;
margin-right:auto;
margin-bottom:7px;
background-color:green;
}
</style>

HTML : HTML:

<div id="elements" class="test_1"></div><span onclick="close_tabs(this)">Div 1</span>
<div id="elements" class="test_1"></div><span onclick="close_tabs(this)">Div 2</span>
<div id="elements" class="test_1"></div><span onclick="close_tabs(this)">Div 3</span>
<div id="elements" class="test_1"></div><span onclick="close_tabs(this)">Div 4</span>

I need that when click over span area, close that div and not all, but i need this works with the same html and i don´t know how do this from jquery 我需要当单击跨度区域时,关闭该div而不是全部关闭,但是我需要使用相同的html进行工作,而且我不知道如何从jquery中执行此操作

Thank´s in advanced, regards 在此先感谢您的问候

Change the function to this: prev() get the previous sibling element, toggle() give elements the ability to hide and show sequentially 将函数更改为: prev()获取上一个同级元素, toggle()使元素能够按顺序隐藏和显示

 function close_tabs(idc) { $(idc).prev().remove(); $(idc).remove(); } 
 #elements { position:relative; width:400px; height:40px; margin-left:auto; margin-right:auto; margin-bottom:7px; background-color:green; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="elements" class="test_1"></div><span onclick="close_tabs(this)">Div 1</span> <div id="elements" class="test_1"></div><span onclick="close_tabs(this)">Div 2</span> <div id="elements" class="test_1"></div><span onclick="close_tabs(this)">Div 3</span> <div id="elements" class="test_1"></div><span onclick="close_tabs(this)">Div 4</span> 

To fix your code: 要修复您的代码:

  • you shouldn't have more, than 1 element with the same id on a page, so remove id="elements" from your div elements 您在页面上不应有超过1个具有相同id元素,因此请从div元素中删除id="elements"
  • in order to easily select your elements with jQuery, I recommend to use a general class, then following more-and-more specific classes, eg class="test test_1" or class="test test_2" 为了使用jQuery轻松选择元素,我建议使用通用类,然后遵循越来越多的特定类,例如class="test test_1"class="test test_2"
  • since you're using jQuery, you don't have to use the onclick attribute on your span elements to handle click events, you can select them more easily with jQuery, so remove the onclick="close_tabs(this)" from your span elements 由于您使用的是jQuery,因此不必使用span元素上的onclick属性来处理单击事件,因此可以使用jQuery更轻松地选择它们,因此从您的span元素中删除onclick="close_tabs(this)"
  • in your CSS, style your elements from more general to the most specific ones, eg instead of styling using #element , use the more general class .test on your elements to style them 在CSS中,将元素的样式从更一般的元素定义到最具体的元素,例如,不要使用#element来设置样式,而应在元素上使用更通用的类.test对其进行样式设置

 // select your elements via the '.test' class // and handle their click events $('.test').on('click', function () { // store the element wrapped with jQuery var element = $(this); // hide the element element.hide(); // hide the element next to it element.next().hide(); }); 
 .test { position: relative; margin-left: auto; margin-right: auto; margin-bottom: 7px; width: 400px; height: 40px; background-color: green; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="test test_1"></div><span>Div 1</span> <div class="test test_2"></div><span>Div 2</span> <div class="test test_3"></div><span>Div 3</span> <div class="test test_4"></div><span>Div 4</span> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM