1

次のようなhtmlがあります。

<fieldset id="question1"> <legend class='legend'>...</legend>

...

<input type="text" name="label_no1" id="label_no1" autocomplete="off">

</fieldset>

Java スクリプトでは、フィールドセットを複製していますが、その要素にアクセスして ID とテキストの一部を変更したいと考えています。

私はこれを試しましたが、うまくいきませんでした:

$('#question1").siblings(".legend").html="any text"; \\ I also tried children

また、フィールドセット内のすべての入力にアクセスできるようにして、ID を変更したいと考えています。何か助けはありますか?

4

5 に答える 5

2

attrメソッドを使用して質問のIDを変更できます。

var q2 = $('#question1').clone();
q2.attr('id', 'question2');

新しい要素の特定の子を編集するには、findメソッドが必要です。

var legend = q2.find('.legend').html('....');
于 2010-06-24T12:33:00.033 に答える
2

これを試して:

$clone = $('#question1').clone();

$clone.attr('id','some_new_id');
// you don't need to search by class or id you can search for tags also,
// so you can get rid of some redundancy and write <legend>…&lt;/legend>
// instead of <legen class="legend">…&lt;/legend>
// siblings won't work with your example, because legend is a child not a sibling
// html= won't work either html on a jQuery object is a method not an attribute
$clone.find('legend').html("New text for the Legend");
于 2010-06-24T12:37:48.467 に答える
1

フィールドセットのクローンを作成して同じ親に追加するには:

var fieldset = $("#question1");    // Get the fieldset
var clone = fieldset.clone();      // Clone it
clone.attr("id", "question2");     // Set its ID to "question2"
clone.appendTo(fieldset.parent()); // Add to the parent

同じIDを持つ2つの要素を持つことはできないため、ツリーに追加する前にIDを変更していることに注意してください。

その中の要素を処理するには、.children()またはセレクターを使用.find()してclone変数を使用し、必要な子/子孫を選択します(親にクローンを追加した後)。たとえばid、入力のをクリーンアップするには、次のようにします。

clone.find('input').each(function() {
    if (this.id) {
        // It has an ID, which means the original had an ID, which
        // means your DOM tree is temporarily invalid; fix the ID
        this.id = /* ...derive a new, unique ID here... */;
    }
});

eachコールバック内では、jQueryインスタンスでthisなく、生の要素であることに注意してください。(したがって、私の設定はthis.id直接です。)要素のjQueryインスタンスを取得したい場合は、取得してから代わりvar $this = $(this);に使用$this.attr("id", ...)します。ただし、IDを変更する以外のことをしない限り、特に必要はありません。


IDの番号の付け直しに関する質問に答えるには、入力要素の実際のIDだけでなく、それらのIDを使用しているものもすべて更新する必要があります。

ただし、入力要素の更新を行うという点では、数値を読み取り、使用されていない数値が得られるまで数値をインクリメントすることで、更新を行うことができます。

clone.find('input').each(function() {
    var id;
    if (this.id) {
        id = this.id;
        do {
            id = id.replace(/[0-9]+$/g, function(num) {
                return String(Number(num) + 1);
            });
        }
        while ($("#" + id).length > 0);
        this.id = id;
    }
});

...元のIDが「input_radio1」の場合は「input_radio2」になりますが、代わりに別の命名規則を使用すると思います。たとえば、さまざまな入力IDの前に質問のIDを付けることができます。

<fieldset id='question1'>
    ...
    <input id=-'question1:input_radio1' />
</fieldset>

...次に、複製されたIDの「question1」を「question2」に置き換えます。(コロン[ :]はIDで完全に有効です。)

ただし、すべての入力にIDを設定することを回避できる場合は、それが最善の方法です。たとえば、マークアップが別の理由でそれを妨げない限り、:を使用するのではなく、を経由して封じ込めinputに関連付けることができます。labelfor

<label>First name: <input type='text' ... /></label>

たくさんのオプション。:-)

于 2010-06-24T12:33:46.830 に答える
0

jQueryhtmlメソッドの構文は次のとおりです。

var result = $(selector).html(); // result now contains the html of the first matched element

@(selector).html('some html markup'); // the html of all matched elements are set to 'some html markup'
于 2010-06-24T12:30:12.410 に答える