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 を使用して、クラス「icon-question-sign」の要素を Clone() し、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 を持つテーブルは、 classquestionitemheaderを持つ要素の祖先の 1 つではありませんicon-question-sign

代わりに次のことができます。

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

ワーキングデモ

于 2013-06-04T16:39:04.850 に答える