HTML代码:
<img id="img" src="mm0.jpg">
<p><button id="beforeText">图片前面插入文字“美女:”</button></p>
<p><button id="beforeHtml">图片前面插入“<strong>美女:</strong>”</button></p>
<p><button id="beforeElement">图片前面插入“美女”标题</button></p>
<p><button id="beforeMultiple">图片前面一次性插入多段内容</button></p>
JS代码:
var eleImg = document.getElementById('img');
// 测试按钮
var eleBtnBeforeText = document.getElementById('beforeText');
var eleBtnBeforeHtml = document.getElementById('beforeHtml');
var eleBtnBeforeElement = document.getElementById('beforeElement');
var eleBtnBeforeMultiple = document.getElementById('beforeMultiple');
// 点击按钮
// 纯文本测试
eleBtnBeforeText.addEventListener('click', function () {
eleImg.before('美女:');
});
// html字符串测试
eleBtnBeforeHtml.addEventListener('click', function () {
eleImg.before('<strong>美女:</strong>');
});
// DOM节点测试
eleBtnBeforeElement.addEventListener('click', function () {
var eleTitle = document.createElement('h4');
eleTitle.innerHTML = '美女:';
eleImg.before(eleTitle);
});
// 多参数测试
eleBtnBeforeMultiple.addEventListener('click', function () {
eleImg.before('1. 美女', ' ', '2. 美女');
});