jQuery after()和InsertAfter()示例

时间:2020-02-23 14:46:04  来源:igfitidea点击:

在本教程中,我们将看到jQuery after和InsertAdter方法。

两者都执行相同的任务,在每个选定的元素后插入文本或者html,但语法是完全不同的。

after()的语法:

$("selectors").after("element to be inserted") ;
Example :
$("div").after("");

InsertAfter()语法:

$("element to be inserted") .insertAfter("selectors");
Example:
$("element to be inserted").insertAfter("div");

让我们通过示例来理解:

<!doctype>
<html>
<head>
  <title>Jquery after and insertAfter example example </title>
  <script src="http://code.jquery.com/jquery-2.2.3.min.js"></script>
</head>
<h2>Jquery after and insertAfter example example</h2>
<style>
   .red{
        border:3px dotted red;
        color:red;
        font-family: arial narrow;
   }
 .blue{
        border:3px dotted red;
        color:blue;
        font-family: arial narrow;
   }
</style>
<script type="text/javascript">
   $(document).ready(function(){
      $("#afterButton").click(function(){
         $("div").after("inserting using after");
      });
$("#insertAfterButton").click(function(){
         $("inserting using insertAfter").insertAfter("div");
      });
$("#reset").click(function(){
         location.reload();
   });
 });
 
</script>
<body>
  <button id="afterButton">Using after</button>
 
  <button id="insertAfterButton">Using insertAfter</button>
<button id="reset">Reset</button>
<div>
 Hello world from theitroad!!!!
   </div>
<div>
 Welcome to JQuery.
  </div>
</body>
</html>