English 中文(简体)
如何打衣和改变?
原标题:How to clone and change id?

我需要打上id,然后在 like后添加一个编号:id1id2等。 每当你打上衣帽子之后,就把衣帽子 after上。

$("button").click(function() {
    $("#id").clone().after("#id");
}); 
最佳回答

$( #cloneDiv ).click(function(){


  // get the last DIV which ID starts with ^= "klon"
  var $div = $( div[id^="klon"]:last );

  // Read the Number from that DIV s ID (i.e: 3 from "klon3")
  // And increment that number by 1
  var num = parseInt( $div.prop("id").match(/d+/g), 10 ) +1;

  // Clone it and assign the new ID (i.e: from num 4 to ID "klon4")
  var $klon = $div.clone().prop( id ,  klon +num );

  // Finally insert $klon wherever you want
  $div.after( $klon.text( klon +num) );

});
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>

<button id="cloneDiv">CLICK TO CLONE</button> 

<div id="klon1">klon1</div>
<div id="klon2">klon2</div>

Scrambled elements, retrieve highest ID

你们有许多带有诸如<代码>klon--5等身份识别器的内容,但斜体(不是按顺序排列)。 这里我们cannot:last:first,因此,我们需要一个机制检索最高级的ID:

const all = document.querySelectorAll( [id^="klon--"] );
const maxID = Math.max.apply(Math, [...all].map(el => +el.id.match(/d+$/g)[0]));
const nextId = maxID + 1;

console.log(`New ID is: ${nextId}`);
<div id="klon--12">12</div>
<div id="klon--34">34</div>
<div id="klon--8">8</div>
问题回答

<>Update: Roko C.Bulijan 指出,在选定四分之二之后,你需要使用.insertAfter插入。 另外,如果你想要在结尾处添加新的编号,而不是从带上多个时间开始,DEMO

http://www.ohchr.org。

   var cloneCount = 1;;
   $("button").click(function(){
      $( #id )
          .clone()
          .attr( id ,  id + cloneCount++)
          .insertAfter( [id^=id]:last ) 
           //            ^-- Use  #id  if you want to insert the cloned 
           //                element in the beginning
          .text( Cloned   + (cloneCount-1)); //<--For DEMO
   }); 

Try,

$("#id").clone().attr( id ,  id1 ).after("#id");

如果你想要自动柜台,请见下文。

   var cloneCount = 1;
   $("button").click(function(){
      $("#id").clone().attr( id ,  id + cloneCount++).insertAfter("#id");
   }); 

这是我最简单的解决办法。

$( #your_modal_id ).clone().prop("id", "new_modal_id").appendTo("target_container");

工作进展

 var i = 1;
 $( button ).click(function() {
     $( #red ).clone().appendTo( #test ).prop( id ,  red  + i);
     i++; 
 });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<div id="test">
  <button>Clone</button>
  <div class="red" id="red">
  </div>
</div>

<style>
  .red {
    width:20px;
    height:20px;
    background-color: red;
    margin: 10px;
  }
</style>

我制定了普遍的解决办法。 以下功能将改变ids子和 clo子名称。 在多数情况下,你需要一字不提地添加“数据-增长”的字面。

function renameCloneIdsAndNames( objClone ) {

    if( !objClone.attr(  data-row-id  ) ) {
        console.error(  Cloned object must have  data-row-id  attribute.  );
    }

    if( objClone.attr(  id  ) ) {
        objClone.attr(  id , objClone.attr(  id  ).replace( /d+$/, function( strId ) { return parseInt( strId ) + 1; } ) );
    }

    objClone.attr(  data-row-id , objClone.attr(  data-row-id  ).replace( /d+$/, function( strId ) { return parseInt( strId ) + 1; } ) );

    objClone.find(  [id]  ).each( function() {

        var strNewId = $( this ).attr(  id  ).replace( /d+$/, function( strId ) { return parseInt( strId ) + 1; } );

        $( this ).attr(  id , strNewId );

        if( $( this ).attr(  name  ) ) {
            var strNewName  = $( this ).attr(  name  ).replace( /[d+]/g, function( strName ) {
                strName = strName.replace( /[[] ]+/g,    );
                var intNumber = parseInt( strName ) + 1;
                return  [  + intNumber +  ] 
            } );
            $( this ).attr(  name , strNewName );
        }
    });

    return objClone;
}
$( #cloneDiv ).click(function(){


  // get the last DIV which ID starts with ^= "klon"
  var $div = $( div[id^="klon"]:last );

  // Read the Number from that DIV s ID (i.e: 3 from "klon3")
  // And increment that number by 1
  var num = parseInt( $div.prop("id").match(/d+/g), 10 ) +1;

  // Clone it and assign the new ID (i.e: from num 4 to ID "klon4")
  var $klon = $div.clone().prop( id ,  klon +num );

  // Finally insert $klon wherever you want
  $div.after( $klon.text( klon +num) );

});
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签