在Javascript中添加位于现有HTML之前的innerHTML [英] Adding innerHTML positioned before the existing HTML in Javascript
本文介绍了在Javascript中添加位于现有HTML之前的innerHTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
代替这种在元素的现有 HTML 之后添加更多 HTML 的方法,
document.getElementById('div1').innerHTML += 'new html';
旧html新html
有没有办法将它插入之前现有的HTML?
document.getElementById('div1').innerHTML += 'new html';
新html旧html
解决方案
是的,很简单:
var oldHTML = document.getElementById('div1').innerHTML;document.getElementById('div1').innerHTML = "New HTML" + oldHTML;
Instead of this method which adds more HTML after the existing HTML of an element,
document.getElementById('div1').innerHTML += 'new html';
<div id="div1">old html new html</div>
Is there a way to insert it before the existing HTML?
document.getElementById('div1').innerHTML += 'new html';
<div id="div1">new html old html</div>
解决方案
Yes, simply:
var oldHTML = document.getElementById('div1').innerHTML;
document.getElementById('div1').innerHTML = "New HTML" + oldHTML;
这篇关于在Javascript中添加位于现有HTML之前的innerHTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文