Your code is cursed but I understand what you're trying to do. Every time you do a recursive div you need to do these escapes:
JS escape: " becomes \" (JS can also use ' and `, but for simplicity let's stick with ") and \ becomes \\
HTML escape: " becomes " and & becomes &
Therefore the code you need is:
<div onclick="showDiv(1, "<div onclick=\"showDiv(2, \&quot;test C\&quot;)\">test B</div>")">test A</div>
Here's how you would add a fourth layer!
<html>
<head>
<script>
function showDiv (divId, dat){
document.getElementById(divId).innerHTML = dat ;
document.getElementById(divId).style.display = "block" ;
}
</script>
</head>
<body>
<div onclick="showDiv(1, "<div onclick=\"showDiv(2, &quot;<div onclick=\\&quot;showDiv(3, \\&amp;quot;test D\\&amp;quot;)\\&quot;>test C</div>&quot;)\">test B</div>")">test A</div>
<div id="1" style="display:none;"></div>
<div id="2" style="display:none;"></div>
<div id="3" style="display:none;"></div>
</body>
</html>
(This is obviously not very readable — this is why inline JS is not used very often)
addEventListener()instead of an inline event would help here.