2

ボタンのセットがあります:

  <div id="button">
      <button id="show_link_group">Show Links</button>
      <button id="show_contact_group">Show Contacts</button>
      <button id="show_update_group">Show Updates</button>
      <button id="show_activity_group">Show Action Items</button>
  </div>

そして、このdivをその後に挿入しようとしています:

<div id="link_group">

      <p1>adsfadsf</p1>
</div>

id="show_link_group" のボタンをクリックした後に挿入することになっています。

$('#show_link_group').click(function() {
  $("<div id='link_group'").after('<div id="button"></div>');
});

クリックしても何も起こりません。私はこのドキュメントに従いました:

http://api.jquery.com/insertafter/

私は何を間違っていますか??

ありがとう!

4

2 に答える 2

2

HTML 文字列ではなく、セレクターを使用して挿入する場所を指定する必要があります。

$("<div id='link_group'><p1>asdfasdf</p1></div>").insertAfter("#button");

DIV が既に DOM にある場合link_groupは、次のことができます。

$("#link_group").insertAfter("#button");

これにより、現在の場所からボタンの後ろに移動します。

于 2014-06-03T02:06:40.990 に答える
1

.after() メソッドが逆になっています。最初に、「どの要素の後に挿入したいのか」を伝える必要があります。次に、その要素の後に挿入するコンテンツを指定します。

あなたはこれを持っています:

$('#show_link_group').click(function() {
  $("<div id='link_group'").after('<div id="button"></div>');
});

あなたはこれを求めている:

var content = "my content";
$('#show_link_group').click(function() {
   $("#button").after(content);
});

これがあなたのソリューションを含むjsbinです。http://jsbin.com/qaxan/1/edit

于 2014-06-03T02:19:57.260 に答える