简体   繁体   English

需要帮助在ASP.NET Web窗体中使用jQuery遍历元素

[英]Need help traversing elements with jQuery in ASP.NET Web Forms

我的页面长什么样

在此处输入图片说明

So the first image is what my page looks like. 所以第一张图片就是我的页面的样子。 I am trying to grab the ID of the table at the top (the table that has the class trackingHistory and ID of ContentPlaceHolder1_ctl00_myRpt_ctl00_0_gdvTH_0) whenever the highlighted button is clicked. 每当单击突出显示的按钮时,我都试图在顶部获取表的ID(具有类trackingHistory和ContentPlaceHolder1_ctl00_myRpt_ctl00_0_gdvTH_0的ID的表)。

Right now I can click the button and fire my Javascript method 'ToggleHistory()' and use jQuery to get the ID of my button. 现在,我可以单击按钮并触发我的Javascript方法“ ToggleHistory()”,然后使用jQuery获取按钮的ID。 (.attr('id')) (.attr( '编号'))

but that's about as far as i can get. 但这是我所能达到的。 I have tried messing around with the closest() and prev() methods from jQuery but haven't had any luck. 我已经尝试过使用jQuery的mostest()和prev()方法,但是还没有运气。 any help would be appreciated. 任何帮助,将不胜感激。

javascript method javascript方法

function ToggleHistory(button)
{
    console.log(button);
    var x = $(button).prev();
    var y = $(button).closest('table').find('.trackingHistory');
    //var z = $(button).closest('.trackingHistory');

    console.log(x);
    console.log(y);
    console.log($(y).attr('id'));
    //console.log(z);
}

i dont want to have to hard code the id because there will be a dynamic amount of these tables and buttons. 我不想对ID进行硬编码,因为这些表和按钮会动态增加。

The <button> and the <table> don't have a direct relationship, but do have the <div> in common. <button><table>没有直接关系,但是确实有<div>共同之处。

<div>
  <table class="toggleHistory"></table>
</div>
<button type="button" onclick="ToggleHistory(this)">click here to hide</button>

The <button> and <div> are immediate siblings, which you can traverse between with .prev() and back with .next() . <button><div>是直接同级,您可以在.prev().next()之间.prev()移动。

$(button).prev()...

Then, the <div> and <table> are .parent() and child ( .children() ): 然后, <div><table>.parent()和child( .parent() .children() ):

$(button).prev().children('.trackingHistory');

function ToggleHistory(button)
{
    console.log(button);

    var $historyTable = $(button).prev().children('.trackingHistory');

    console.log($historyTable.attr('id'));
}

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

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