0

给定以下布局:

<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 来 Clone() 具有“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/

4

1 回答 1

0

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

您可以改为:

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

工作演示

于 2013-06-04T16:39:04.850 回答