【发布时间】:2020-07-20 14:25:30
【问题描述】:
我必须整合几个 html 类型标签(例如:<div id = "mysection"> <div class = "mycontainer"> <h1> mytitle </h1> </div> </div>)来格式化数据(这里是“mytitle”)在 javascript 中输出
所以,我的 HTML 中有 <span id="datahere"></span>
我需要编辑的js是:
function Bigdata(title, subtitle, type) {
const mydata = document.getElementById('datahere');
mydata.innerHTML += title + subtitle + (type ? ' <strong>(type is ' + type + ')</strong>' : '') +'<br/>';
}
// element in which the data is initialized, visible in another <span>
const sprit = document.getElementById('sprit');
// when a given message is received
sprit.addEventListener(SpritEvents.MessageSprit, ({ data }) => {
Bigdata('Sprit', data.text, data.type);
// if there are actions, we offer links
if (data.actions) {
var links = '';
for (var i = 0; i < data.actions.length; i++) {
if (i > 0) {
links += ', ';
}
let act = data.actions[i];
links += '<a data-val="' + act.value + '">' + act.title + '</a>';
}
Bigdata('Sprit', links);
}
});
如果有操作,我无法整合必须“包含”数据(标题、副标题、类型)的标签 WHITH 链接...
如果我像这样添加我的标签: mydata.innerHTML += '<div id = "mysection"> <div class = "mycontainer"><h1>' + title + '</h1>' + subtitle + (type ? ' <strong>(type =' + type + ')</strong>' : '') +'<br/></div></div>';
不包围整体,div 和 h1 是重复的(围绕在一侧的标题、副标题、类型和其他链接上)。
我不习惯使用纯javascript...希望您能帮助我
【问题讨论】:
-
在
mydata.innerHTML += '<div id = "mysection"> <div class = "mycontainer"><h1>' + title + '</h1>' + subtitle + (type ? ' <strong>(type =' + type + ')</strong>' : '') +'<br/>';你没有关闭你的div标签 -
确实 ... mydata.innerHTML += '' 但不要改变任何东西
' + title + '
' + subtitle + (type ? ' (type =' + type + ')' : '') +'
标签: javascript html insert innerhtml