jQuery 遍历查找到的每一个结果

![jQuery 遍历查找到的每一个结果](https://img-blog.csdn.net/20170715204959524?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzYzNTg5NDA=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast) demo要求:找到每个button,设置对应的innerhtml内容为#target+对应的数字; 准备工作:引入jQuery 代码如下:
<div class="container-fluid">
  <h3 class="text-primary text-center">jQuery Playgroundh3>
  <div class="row">
    <div class="col-xs-6">
      <h4>#left-wellh4>
      <div class="well" id="left-well">
        <button class="btn btn-default target" id="target1">button>
        <button class="btn btn-default target" id="target2">button>
        <button class="btn btn-default target" id="target3">button>
      div>
    div>
    <div class="col-xs-6">
      <h4>#right-wellh4>
      <div class="well" id="right-well">
        <button class="btn btn-default target" id="target4">button>
        <button class="btn btn-default target" id="target5">button>
        <button class="btn btn-default target" id="target6">button>
      div>
    div>
  div>
div>
<script>
$("button").each(function (i){
  $(this).html("target"+(i+1));
});
script>

比较笨的方法:

<script> 
 $("button:eq(0)").html("#target1");
 $("button:eq(1)").html("#target2");
  $("button:eq(2)").html("#target3");
  $("button:eq(3)").html("#target4");
  $("button:eq(4)").html("#target5");
  $("button:eq(5)").html("#target6");
script>

你可能感兴趣的:(jquery)