Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

jQuery: How do I rewrite .after( content, content )?

I've got this form working, but according to my previous question it might not be supported: it isn't in the docs either way -- but the intention is pretty obvious in the code.

$(".section.warranty .warranty_checks :last").after(
  $('<div class="little check" />').click( function () {
      alert('hi')
  } )
  , $('<span>OEM</span>')  /*Notice this (a second) argument */
);

What this does is insert <div class="little check"> with a simple .click() callback, followed by a sibling of <span>OEM</span>. How else can I write this then? I'm having difficulty conjuring something working by chaining any combination of .after(), and .insertAfter()?

I would expect this to work, but it doesn't:

$(".section.warranty .warranty_checks :last").after(
  $('<div class="little check" />').click( function () {
      alert('hi')
  } ).after ( $('<span>OEM</span>')  )
);

I would also expect this to work, but it doesn't:

$(".section.warranty .warranty_checks :last").after(
  $('<span>OEM</span>').insertAfter(
    $('<div class="little check" />').click( function () {
       alert('hi')
    } )
  );
);

-> Please see my jsfiddle for examples (test case)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This may be the naive answer:

$(".section.warranty .warranty_checks :last").after(
    $('<span>OEM</span>')
).after(
    $('<div class="little check" />').click( function () {
     alert('hi')
    }
);

Seems to work...

EDIT: have I missed the point here? http://jsfiddle.net/wFx9Z/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...