所以我有一个小的html渲染器,返回一个div与h1和按钮标签嵌套在它。
for(let each in infoForThatDay) {
if(infoForThatDay[each]['start'] == hour.substring(0, 2)) {
let htmlRender = `
<div class='flex-boxes'>
<h1>${infoForThatDay[each]['title']}</h1>
<button onclick="${console.log('at least it works until here')}">დაჯავშნე ახლა</button>
</div>
`;
html += htmlRender;
}
};
let wrapper = document.querySelector('.users');
wrapper.innerHTML = html;
对infoForThatDay中的所有元素记录一次'at least it works until here'。我不明白我做错了什么。提前感谢<3
您不将onclick
属性内的语句放在${}
中,因为您正在这样做,它作为JS函数的一部分执行。你的代码应该是这样的
let htmlRender = `
<div class='flex-boxes'>
<h1>${infoForThatDay[each]['title']}</h1>
<button onclick="console.log('at least it works until here')">დაჯავშნე ახლა</button>
</div>
`;
console.log
代码作为字符串打印到您正在渲染的HTML中,并且不会在您的代码运行时立即执行。