jquery - Javascript insertAdjacentHTML not working - Stack Overflow

In fiddle - , does not work as expectedvar x = document.createElement("button");x.textConte

In fiddle - /, does not work as expected

var x = document.createElement("button");
x.textContent = "byyyyy";
elx = document.getElementById("el");
elx.insertAdjacentHTML('afterend', x); 

In fiddle - http://jsfiddle/u3bmytnL/, does not work as expected

var x = document.createElement("button");
x.textContent = "byyyyy";
elx = document.getElementById("el");
elx.insertAdjacentHTML('afterend', x); 
Share Improve this question edited Feb 6, 2015 at 10:53 nikhil rao asked Feb 6, 2015 at 10:43 nikhil raonikhil rao 3913 gold badges6 silver badges9 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 9

That's should resolve:

var x = document.createElement("BUTTON");
var text = document.createTextNode("byyyy");
x.appendChild(text);
elx = document.getElementById("el");
elx.insertAdjacentHTML('afterend', x.outerHTML); 

The insertAdjacentHTML needs a string as second parameter, and you was passing a DOM element, so you need to get the HTML string

It is because insertAdjacentHTML method takes a string instead of an element node reference.

MDN insertAdjacentHTML

You should put a string as the second parameter

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743695117a4491625.html

相关推荐

  • jquery - Javascript insertAdjacentHTML not working - Stack Overflow

    In fiddle - , does not work as expectedvar x = document.createElement("button");x.textConte

    1天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信