简体   繁体   English

如何扫描类中的所有元素以查找特定的ID,并为不同的ID重复该过程?

[英]How do I scan all elements within a class to find a specific id and repeat the process for different ids?

I am quite new to jquery, so please excuse me if I'm doing something stupid: 我对jquery还是很陌生,所以如果我做蠢事,请原谅:

I have a table where each td belongs to the same class "original" and each one has a unique id. 我有一个表,其中每个td都属于同一类“原始”,并且每个都有唯一的ID。

<table id="main">
        <tr>
            <td style="cursor: pointer" class="original" id="1">
                one
            </td>
            <td style="cursor: pointer" class="original" id="2">
                two
            </td>
        </tr>
        ...
        ...
</table>

I also have a div where all elements are part of a separate class "onclick" and each element has another unique id that is connected to each td from the table. 我也有一个div,其中所有元素都是单独的类“ onclick”的一部分,每个元素都有另一个唯一的ID,该ID与表中的每个td连接。

<div id="second">
        <div class="onclick" id="1click">
            Extra info about 1
            <span class="close" style="cursor: pointer">&times;</span>
        </div>
        <div class="onclick" id="2click">
            Extra info about 2
            <span class="close" style="cursor: pointer">&times;</span>
        </div>
        ...
        ...
</div>

When I click on each individual td, I want to display its corresponding div, so if I click on "1" then "1click" shows up. 当我单击每个单独的td时,我要显示其相应的div,因此,如果单击“ 1”,则会显示“ 1click”。 I currently have this jquery code that successfully does this, but only for the first pair of elements, so it doesn't work for anything beyond id="1" and id="1click". 我目前拥有成功完成此操作的jquery代码,但仅适用于第一对元素,因此它对id =“ 1”和id =“ 1click”以外的任何内容均无效。

$(function () {
    $(".original").click(function() {
        var divname = this.id;
        if ($(".onclick").attr("id") == divname + 'click'){
            var clickname= "#" + divname + "click";
            if ($(".hide").css("display") == 'none'){
                $(clickname).toggle();
            }
        }
    }
}

How can I make it so that it searches each element of the table to find a match with each element of the div? 我如何才能使它搜索表中的每个元素以找到与div的每个元素匹配的内容?

Thanks, and sorry if this is a stupid mistake. 谢谢,如果这是一个愚蠢的错误,对不起。

I'm finding it difficult understanding what you're trying to do and would love to help but I need to try and understand it better. 我发现很难理解您要做什么,并且很乐意为您提供帮助,但我需要尝试并更好地理解它。

1) You can search using .each() which will scan all the elements and you can use a conditional statement to find the one which is needed: Ie 1)您可以使用.each()进行搜索,这将扫描所有元素,并且您可以使用条件语句来查找所需的条件:即

$(document).ready(function(){
  $("td").each(function() {
    if ($(this).attr("id") == "2") {
        var element = $("#"+$(this).attr("id")+"click");
        // do stuff with element
    }
  })
});

I created a small example which I think helps your scenario on JSFiddle: https://jsfiddle.net/keuhfu36/ 我创建了一个小示例,我认为它对您在JSFiddle上的场景有帮助: https ://jsfiddle.net/keuhfu36/

$(document).ready(function(){
  $("#i2click").click(function() {
    alert("hello");
  });
  $("td").each(function() {
    if ($(this).attr("id") == "i2") {
      $("#"+$(this).attr("id")+"click").click();
    }
  });
});

2) I recommend you change the div values from id='1' and id='2' like i did in the example to a value which starts with a letter ie id="i1" and id="i2" which makes it a lot less of a pain to work with. 2)我建议您像在示例中一样将div值从id='1'id='2'更改为以字母开头的值,即id="i1"id="i2" 很多痛苦与工作的较少。

Your script should be like , 您的脚本应该像

$(".original").click(function() {
  var divId = this.id,
      targetDiv = $("#"+divId+"click");
  $(".onclick:visible").hide();
  targetDiv.show();
});

Try the following code: 尝试以下代码:

 $(function(){ $('.onclick').hide(); $(".original").click(function() { var divname = this.id; $('#'+divname+'click').toggle(); }); }); 

 $(function () { $(".original").click(function() { // Targeted Div Name var divname = "#"+ $(this).attr('id') + "click"; // Toggle the div, as Id would be unique in the page $(divname).toggle(); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="main"> <tr> <td style="cursor: pointer" class="original" id="1"> one </td> <td style="cursor: pointer" class="original" id="2"> two </td> </tr> </table> <div id="second"> <div class="onclick" id="1click"> Extra info about 1 <span class="close" style="cursor: pointer">&times;</span> </div> <div class="onclick" id="2click"> Extra info about 2 <span class="close" style="cursor: pointer">&times;</span> </div> </div> 

IMHO, you should ditch the IDs altogether and just use the classes along with their indexes to achieve the desired effect much simpler, like this: 恕我直言,您应该完全放弃ID,而只使用类及其索引来实现所需的效果就更简单了,如下所示:

 $('#main').on('click', '.original', function() { var index = $('.original').index($(this)); // find the index of the clicked td $('.onclick').hide().eq(index).show(); // hide all the divs then show the one at the same index as the clicked td }) .on('click', '.close', function() { $('.onclick').hide(); // hide all the divs when "close" is clicked }); 
 .onclick { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="main"> <tr> <td style="cursor: pointer" class="original"> one </td> <td style="cursor: pointer" class="original"> two </td> </tr> ... ... </table> <div id="second"> <div class="onclick"> Extra info about 1 <span class="close" style="cursor: pointer">&times;</span> </div> <div class="onclick"> Extra info about 2 <span class="close" style="cursor: pointer">&times;</span> </div> ... ... </div> 

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

相关问题 我如何选择全部 <th> 具有特定ID的表中“sortasc”类的元素? - How do I select all <th> elements of class “sortasc” within a table with a specific id? 如何使用 document.querySelector() 查找具有特定 ID 的 div 中具有特定名称 class 的元素 - How to use document.querySelector() to find elements with a specific class name that are within a div with a specific ID 我有一个包含不同div ID的数组,我如何从aray javascript / jquery中定位特定的ID - I have an array containing different div IDs how do i target specific IDs from within the aray javascript/jquery Typescript禁用特定ID中具有类的所有元素 - Typescript disable all elements within specific ID that has a class 如何找到具有特定属性的所有元素? - How do I find all elements with a specific attribute? 如何在 Beautiful Soup 和 Selenium 中查找特定 div ID 中的所有元素 - How to find all elements within specific div ID in Beautiful Soup and Selenium 如何选择特定类名称的所有元素? - How do I select all elements of a specific class name? 如何为具有不同类名的元素重复 Javascript 表达式 - How can I repeat Javascript expression for elements with different class names 如何删除已知ID元素中的特定子元素? - How do I remove specific child elements within an element of known ID? 赛普拉斯在元素列表中查找特定 ID - Cypress Find specific ID within list of elements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM