简体   繁体   English

使用JQuery获取表onClick的单元格索引

[英]Get cell index of table onClick with JQuery

$('.details').click(function(){
  $(this).index()+1).toggleClass('.details, .overlay-wrapper');
});

Each table cell has some details/content in (hidden). 每个表单元格中都有一些详细信息/内容(隐藏)。 Whenever a user selects the cell I need to pass through the index so only the details/div within the cell is shown. 每当用户选择单元格时,我都需要通过索引,因此仅显示该单元格中的详细信息/格。

Every single cell in the table has a div/details within the cell. 表格中的每个单元格在该单元格中都有一个div /细节。 So I need to only toggle on and off the div within the correct cell. 因此,我只需要在正确的单元格内打开和关闭div。 At the moment it toggles every single details div on the page. 目前,它会切换页面上的每个详细信息div。

Thank you 谢谢

JsFiddle is below with the html. JsFiddle位于html的下方。

http://jsfiddle.net/t6yczwuo/ http://jsfiddle.net/t6yczwuo/

You have several problems with the code shown: 您显示的代码有几个问题:

  • Unmatched bracket 无与伦比的支架
  • Incorrect parameters for toggleClass (no . and no comma) 对于toggleClass不正确的参数(无.并没有逗号)

Without HTML this is all guesswork, but you seldom need the index to work with related cells. 没有HTML,这全是猜测,但是您很少需要索引来处理相关单元格。 The following mockup use closest() to find the TD parent, then find() to find another related cell in the same TD: 下面的模型使用closest()查找TD父对象,然后使用find()查找同一TD中的另一个相关单元格:

$('.details').click(function(){
    $(this).toggleClass("details overlay-wrapper").closest('td').find('.someother').toggle();
});

JSFiddle: http://jsfiddle.net/TrueBlueAussie/gj2zz8po/ JSFiddle: http : //jsfiddle.net/TrueBlueAussie/gj2zz8po/

This example simply toggle the classes you specified on the details div, and hides/shows a related div within the same TD. 本示例仅切换您在detail div上指定的类,并在同一TD中隐藏/显示相关的div。

If you use this JSFiddle as the start of your example we can customise the code to match your situation. 如果您使用此JSFiddle作为示例的开始,我们可以自定义代码以匹配您的情况。

Update for your new HTML: 更新您的新HTML:

$('.details-more').click(function (e) {
    e.preventDefault();
    $(this).closest('td').find('.overlay-wrapper .details').toggle();
});

http://jsfiddle.net/TrueBlueAussie/gj2zz8po/2/ http://jsfiddle.net/TrueBlueAussie/gj2zz8po/2/

Note: The e.preventDefault() should be used, even on bookmark links, to stop the page move to the top when clicked on a longer page. 注意:即使在书签链接上,也应使用e.preventDefault()来阻止单击较长页面时将页面移到顶部。

You need .next() and not index() . 您需要.next()而不是index()

.toggleClass() accepts just a single class name without a . .toggleClass()仅接受单个类名,而没有. . Also, toggling the class on which you are using any kind of selector is not recommended. 另外,也不建议切换使用任何选择器的类。

$('.details-more').click(function(e){
    e.preventDefault();
    $(this).next('.overlay-wrapper').find('div').toggleClass('details');

    //Instead of find('div') you could use a specific class selector - 
    //find('.targetHidden') provided this class remains static throughout the html.
});

Updated Fiddle 更新小提琴

HTML 的HTML

<table style="width:100%">
  <tr>
    <td>Header</td>
    <td>Header</td> 
    <td>Header</td>
  </tr>
  <tr>
    <td>
        <a href="#" class="details-more">Details</a>
        <div class="overlay-wrapper details">
            <div class="">THIS IS THE SOME DETAILS OR CONTENT</div></div>
    </td>
    <td>
        <div class="details"></div>
    </td>
    <td>
        <a href="#" class="details-more">Details</a>
        <div class="overlay-wrapper details">
            <div class="">THIS IS SOME MORE CONTENT</div></div>
    </td>
  </tr>
  <tr>
    <td>
        <div class="details"></div>
    </td>
    <td>
        <a href="#" class="details-more">Details</a>
        <div class="overlay-wrapper details">
            <div class="">SOME TEST RANDOM STUFF</div></div>
    </td>
    <td>
        <div class="details"></div>
    </td>
  </tr>
  <tr>
    <td>
        <div class="details"></div>
    </td>
    <td>
        <div class="details"></div>
    </td>
    <td>
        <a href="#" class="details-more">Details</a>
        <div class="overlay-wrapper details">
            <div class="">SOME MORE CONTENT</div></div>
    </td>
  </tr>
  <tr>
    <td>
        <div class="details"></div>
    </td>
    <td>
        <div class="details"></div>
    </td>
    <td>
        <div class="details"></div>
    </td>
  </tr>
</table>

CSS 的CSS

table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
}
th, td {
    padding: 15px;
}

.details {display:none;}

Javascript Java脚本

$('.details-more').click(function(){
  $(this).next('.overlay-wrapper').toggleClass('details');
});

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

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