Pregunta

Cuando uso la función Appendbox a continuación, la primera vez funciona bien y lo mismo con Remoutbox, pero la segunda vez no funciona correctamente: duplica mi contenido.

¿Qué está mal?

function appendBox( id )
{
    $("#listContainer").append("<div id=\"appendbox-"+ id +"\"><a href=\"javascript:removeBox("+ id +");\">remove</a></div>");
    $("#addBox-"+ id ).slideUp();
}
function removeBox( id )
{
    $("#listMyBoxs").append("<div id=\"addBox-"+ id +"\"><a href=\"javascript:appendBox("+ id +");\">tilføj</a></div>");
    $("#appendbox-"+ id ).slideUp();
}

este es mi html cuando intento esta prueba

<div style="height: 200px; border: 1px solid black;">

    <div id="listContainer"></div>

    <div id="listMyBoxs">

        <div id="addBox-1">
            <a href="javascript:appendBox( 1 );">tilføj</a>
        </div>

    </div>

</div>
¿Fue útil?

Solución

Cambiaría la forma en que lo estás haciendo. A una solución más típica le gustaría esto:

<div id="outer">
  <div id="listContainer"></div>
  <div id="listMyBoxes">
    <a href="#">tilføj</a>
  </div>
</div>

con:

#outer { height: 200px; border: 1px solid black; }
#outer a { display: block; }

y

$(function() {
  $("#listContainer a").live("click", function() {
    $(this).slideUp(function() {
      $(this).appendTo("#listMyBoxes").text("remove").slideDown();
    });
    return false;
  });
  $("#listMyBoxes a").live("click", function() {
    $(this).slideUp(function() {
      $(this).appendTo("#listContainer").text("tilføj").slideDown();
    });
    return false;
  });
});

Ajustar según sea necesario. Si necesita contenido más complejo que un enlace simple, puede encerrarlo en un DIV y ajustar según sea necesario. Los principios son todos iguales.

Otros consejos

If you want to move an existing element, try:

$("#appendbox-"+ id ).appendTo('#listMyBoxs');

Using $("<div></div>") or .append("<div></div>") creates a new element.

The same issue here, I used to append content to page using javascript and jQuery, it works fine at Chrome, but when trying it with Firefox it duplicates the appended content.

Here is an example: duplicate appeneded content

I use this code to append content to a div:

function PlayGames() {
    $(".gameActions, #flash-game").show();
    $(".TheGame").show().append("<object id='flash-game' type='application/x-shockwave-flash' height='100%' width='100%' frameborder='0' scrolling='no' data='URL'>");
        }

Don't know why this happens only in Firefox and IE sometimes.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top