父表的选择器问题

时间:2013-06-04 16:32:05

标签: jquery jquery-selectors

给出以下布局:

<table class="questionitem" id="RichSurvey_1_Question1_QuestionTable_1">
    <tbody>
        <tr>
            <td>
                <table class="questionitemheader" id="RichSurvey_1_Question1_HeaderTable1">
                    <tbody>
                        <tr>
                            <td style="width: 99%;"><span><div>Please identify the client's entity type</div><span class="questionvalidationmark">*</span></span>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </td>
        </tr>
        <tr>
            <td>
                <table class="answeritem">
                    <tbody>
                        <tr>
                            <td>
                                <table>
                                    <tbody>
                                        <tr>
                                            <td valign="top" colspan="1"><span id="RichSurvey_1_Question1__as9267__ai1_as9267"><select name="RichSurvey$1$Question1$_as9267$_ai1_as9267$AnswerItemDropDownList" class="answerdropdown" id="RichSurvey_1_Question1__as9267__ai1_as9267_AnswerItemDropDownList" onchange="triggerLink=true;__doPostBack('RichSurvey$1$PostbackLinkTrigger','')">
                                                    <option value="-1">[Select an answer]</option>
                                                    <option value="1">Listed company (or qualifying subsidiary)</option>
                                                    <option value="2">Regulated entity (or qualifying subsidiary)</option>
                                                    <option value="3">Non-Listed company </option>
                                                    <option value="4">Unincorporated organisation</option>
                                                    <option value="5">Trust, Foundation or similar</option>
                                                    <option value="6">Government entity</option>
                                                    <option value="7">Individual</option>
                                                    <option value="8">DTTL Member Firm</option>
                                                    <option value="9">Pension Scheme</option>
                                                    <option value="10">Fund</option>

                                                </select></span>
                                            </td>
                                            <td valign="top"><span id="RichSurvey_1_Question1__as9267__ai391_as9267"><div>
                                                    <i class="icon-question-sign pull-right" data-help="<h5>Test Help Content</h5>Test Help Content">
                                                </i></div></span>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </td>
        </tr>
    </tbody>
</table>                                        </tr>
                                </tbody></table></td>
                            </tr>
                        </tbody></table>

我正在尝试使用jQuery克隆()带有“icon-question-sign”类的元素,并将克隆放在表格的第一个元素中,其ID为“RichSurvey_1_Question1_HeaderTable1”。

我遇到的问题是这个表的id是未知的,所以我需要做的是,向后导航DOM并使用“questionitemheader”类找到我元素之前的id。

目前我的桌子选择器出现问题,并且想知道是否有人可以提供帮助。

我的jquery(尽可能少),如下:

$('.icon-question-sign').each(function (i, obj) {
     // Create a copy of the element with data and event handlers
     var newObj = $(obj).clone(true);
     var headerTable = $(obj).parents(".questionitemheader")[0];
     alert($(headerTable).attr('id'));


 });

这是我的问题的JSFiddle:

http://jsfiddle.net/24ZGF/59/

1 个答案:

答案 0 :(得分:0)

questionitemheader的表不是具有类icon-question-sign的元素的祖先之一。

您可以改为:

var headerTable = $(this).closest(".questionitem").find('.questionitemheader');

<强> Working Demo