简体   繁体   English

在jQuery上选择特定的子级

[英]Selecting specific children on jQuery

I'm trying to hide the divs on click using .click() event and .hide() method. 我正在尝试使用.click()事件和.hide()方法在单击时隐藏div。

The thing is each one has a different ID that was 'chosen' by the user, so i can't just write the code for each case. 关键是每个人都有一个由用户“选择”的不同ID ,所以我不能只为每种情况编写代码。

Example: 例:

HTML 的HTML

<body>
      <div id="box">
           <div id="div1"></div>
           <div id="div2"></div>
           <div id="div3"></div>
      </div>
</body>

jQuery jQuery的

$(document).ready(function() {
     $(---selector for all divs 1,2,3----).click(function) {
          $(----specificDivIClicked----).hide();
     });
});

Basicaly, i'm trying to find a way to delete only the div i click on. 基本上,我正试图找到一种方法来仅删除我单击的div。

Try to select all divs inside box div : 尝试在div box选择所有div:

$(document).ready(function()
{
    $('#box div').click(function(){
        $(this).hide();
    });
});

 $(document).ready(function() { $('#box div').click(function(){ $(this).hide(); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="box"> <div id="div1">div1</div> <div id="div2">div2</div> <div id="div3">div3</div> </div> 

Thy blanks have been filled: 您的空白已填满:

$(document).ready(function() {
     $('[id^="div"]').click(function() {
          //------------------------^---- You are missing (
          $(this).hide();
     });
});

Snippet 片段

 $(document).ready(function() { $('[id^="div"]').click(function() { $(this).hide(); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="box"> <div id="div1">div1</div> <div id="div2">div2</div> <div id="div3">div3</div> </div> 

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

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