<button οnclick="innerHTML_()">innerHTML</button>
<div id="innerHTML">我是内容</div>
<button οnclick="beforeBegin()">beforeBegin</button>
<div id="beforeBegin">我是内容!</div>
<button οnclick="afterBegin()">afterBegin</button>
<div id="afterBegin">我是内容!</div>
<button οnclick="beforeEnd()">beforeEnd</button>
<div id="beforeEnd">我是内容!</div>
<button οnclick="afterEnd()">afterEnd</button>
<div id="afterEnd">我是内容!</div>
<br/><br/>
<script type="text/javascript">
var dom = {
innerHTML: document.getElementById('innerHTML'),
beforeBegin: document.getElementById('beforeBegin'),
afterBegin: document.getElementById('afterBegin'),
beforeEnd: document.getElementById('beforeEnd'),
afterEnd: document.getElementById('afterEnd')
};
var buttonHtml = '<button type="button" οnclick="alert()">点我试试</button>';
function innerHTML_() {
dom.innerHTML.innerHTML += buttonHtml;
}
function beforeBegin() {
dom.beforeBegin.insertAdjacentHTML('beforeBegin', buttonHtml);
}
function afterBegin() {
dom.afterBegin.insertAdjacentHTML('afterBegin', buttonHtml);
}
function beforeEnd() {
dom.beforeEnd.insertAdjacentHTML('beforeEnd', buttonHtml);
}
function afterEnd() {
dom.afterEnd.insertAdjacentHTML('afterEnd', buttonHtml);
}
</script>
js插入标签位置
最新推荐文章于 2024-08-11 00:22:53 发布