「JavaScript」- 操作 HTML 文档

  CREATED BY JENKINSBOT

解决方案

获取元素属性

var href = aTag.href
var value = input.value

追加 HTML 元素、修改 HTML 内容

原始 JavaScript 代码

//get the target div you want to append/prepend to
var someDiv = document.getElementById("targetDiv");

//append text
someDiv.innerHTML += "Add this text to the end";

//prepend text
someDiv.innerHTML = "Add this text to the beginning" + someDiv.innerHTML;

// after
someDiv.outerHTML += "Add this text to the end";

// before
someDiv.outerHTML = "Add this text to the end" + someDiv.outerHTML;

使用 jQuery 类库

向元素内部的最开始追加新元素:.prepend()

<div class="container">
  <div class="inner">Hello</div>
</div>

$( ".inner" ).prepend( "<p>Test</p>" );

<div class="container">
  <div class="inner">
    <p>Test</p>
    Hello
  </div>
</div>

参考文献

How can I implement prepend and append with regular JavaScript? – Stack Overflow