5

Elementdart:htmlのベースオブジェクトにelementsは、 の実装であるプロパティがありList<E>ます。このaddメソッドは要素を DOM に追加します。要素を DOM の前に追加または挿入したいと考えています。の挿入メソッドはどれElementListも実装されていません。これどうやってするの?

4

2 に答える 2

5

たとえば、次の HTML スニペットがあるとします。

<body>
    <div id='test'></div>
</body>

これを行うと、div の前に要素を挿入できます。

// Get the sibling that we want to insert before.
final el = query('#test');

// From the parent element, we call insertBefore, referencing the
// sibling that we want to insert the new element before.
document.body.insertBefore(new Element.tag('p'), el);

これで、HTML は次のようになります。

<body>
    <p></p>
    <div id='test'></div>
</body>
于 2012-09-23T19:44:23.310 に答える
5

ジョンの答えに加えて。次の構文も使用できます。

final el = query('#test');

// If you want an element
el.insertAdjacentElement('beforebegin', new Element.tag('p'));
// If you want to insert HTML tags
el.insertAdjacentHTML('beforebegin', '<p>Hello</p>');
// Insert just text
el.insertAdjacentText('beforebegin', 'Hello there');

位置引数を使用できます: beforebeginafterbeginbeforeendafterend これらは、他の要素の先頭の前、要素の先頭のすぐ内側、要素の末尾のすぐ内側、または要素の末尾のすぐ外側に配置されます。それぞれ。

于 2012-09-23T19:51:01.600 に答える