最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Replace instead of append based on user input? - Stack Overflow

programmeradmin0浏览0评论

This function currently works for appending an img in a div. But I need a replace method instead of multiple img divs stacking on top of each other.

I'm open to solutions in jQuery as well.

function getImg(){

    var url=document.getElementById('txt').value;
    var div=document.createElement('div');
    var img=document.createElement('img');

    img.className="img-responsive";

    img.src=url;

    div.appendChild(img);

    document.getElementById('gif-btn').innerHTML=""; <!-- Correct answer -->
    document.getElementById('gif-btn').appendChild(div);

    return false;
}

Thanks in advance for any and all help. Much appreciated!

This function currently works for appending an img in a div. But I need a replace method instead of multiple img divs stacking on top of each other.

I'm open to solutions in jQuery as well.

function getImg(){

    var url=document.getElementById('txt').value;
    var div=document.createElement('div');
    var img=document.createElement('img');

    img.className="img-responsive";

    img.src=url;

    div.appendChild(img);

    document.getElementById('gif-btn').innerHTML=""; <!-- Correct answer -->
    document.getElementById('gif-btn').appendChild(div);

    return false;
}

Thanks in advance for any and all help. Much appreciated!

Share Improve this question edited Jan 8, 2014 at 14:21 dMaller asked Jan 6, 2014 at 20:26 dMallerdMaller 1131 silver badge12 bronze badges 3
  • To replace you just remove the old image after appending the new one. – Barmar Commented Jan 6, 2014 at 20:28
  • There's actually a replaceChild method in javascript that seems to fit the bill ? – adeneo Commented Jan 6, 2014 at 20:29
  • 1 Change your .appendChild()s to .innerHTML= 'stuff' – SenorAmor Commented Jan 6, 2014 at 20:29
Add a ment  | 

3 Answers 3

Reset to default 5

Before you append, you can clear out the div, then append the new one

function getImg(){

    var url=document.getElementById('txt').value;
    var div=document.createElement('div');
    var img=document.createElement('img');

    img.className="img-responsive";

    img.src=url;

    div.appendChild(img);

    document.getElementById('gif-btn').innerHTML = ""; // <-- Clears the gif-btn div
    document.getElementById('gif-btn').appendChild(div);

    return false;
}

Using jQuery

var url = $("#txt").val();
var image = $("<img>").attr("src", url).addClass("img-responsive");
var container = $("<div></div>");

// add the image to the <div> container
container.append(image);
// set the contents of gif-btn
$("#gif-btn").html(container);

... or better yet, you could do a simple fade-out, fade-in...

// replace the last line with this
$("#gif-btn").fadeOut("fast", function() { 
  $("#gif-btn").html(container); $("#gif-btn").fadeIn(); 
});

This can be done directly with replaceChildren (no need for setting innerHTML).

function getImg(){

    let parent=document.querySelector('#gif-btn');
    let oldChild=document.createElement('div');
    oldChild.innerText = 'old child'
    let newChild=document.createElement('div');
    newChild.innerText = 'new child';

    parent.appendChild(oldChild);
    parent.replaceChildren(newChild); // replace with 1+ elements
}
getImg();
<div id="gif-btn"></div>

发布评论

评论列表(0)

  1. 暂无评论