我想知道是否有更好的方法来做innerHTML比我在这里做的。我这样做的方式导致问题,因为我不能有三重嵌套引号。另一方面,真的很难看和管理。谢谢!有没有更好的方法来做innerHTML?
function buttonClicked(buttonValue)
{
switch (buttonValue)
{
case 1:
soundFolders.innerHTML = "<li onClick='buttonClicked(11);'>Thunder 1</li> <li onClick='buttonClicked(13);'>Light Rain 1</li> <li onClick='buttonClicked(0);'>Back</li>";
break;
case 11:
if(!thunder1Control)
{
thunder1.play();
var thunder1Control = document.createElement("li");
soundList.appendChild(thunder1Control);
thunder1Control.innerHTML = "<h3>Thunder 1</h3> <button class='stopSound' onClick='thunder1.pause(); thunder1.currentTime=0; thunder1Control.parentNode.removeChild(thunder1Control); '>X</button> <button class='volDown' onClick='thunder1.volume -= 0.25;'>-</button> <button class='volUp' onClick='thunder1.volume += 0.25;'>+</button>";
thunder1Control.setAttribute("class", "playingSound");
}
P.S.你们知道为什么thunder1Control.parentNode.removeChild(thunder1Control)
不工作?
如果'thunderControl'有'.parentNode',''.removeChild()'应该可以工作。是否有理由替换元素的所有内容?你不能只更新特定的节点吗? –
寻找不显眼的javaScript,它可能会帮助您的整体设计。 – Rayshawn
将所有这些onclick处理程序移出HTML定义。 –