我正在尝试使用脚本中的 document.write() 编写一个 div。我似乎能够写一个空白的 div,但每当我向 div 添加一个类或任何属性时,它就不再写任何东西了。
这是我要运行的代码。
document.write("<div class="new_prod_box">");
document.write("<a href="details.html">");
document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
document.write("</a>");
document.write("<div class="new_prod_bg">");
document.write("<a href="details.html"><img src="images/thumb1.gif" alt="" title="" class="thumb" border="0" /></a>");
document.write("</div>");
document.write("</div>");
我加载 xml 没有问题,因为我已经对此进行了测试,并且我在我希望写入它的部分运行了一个脚本,并且它在该区域写入得很好。我还使用普通的 div 进行了测试,它工作正常,只是在将类分配给该 div 时似乎是个问题。
我已经运行了这段代码,看看是否是 document.write() 写入 div 标签时出现问题,它工作正常,但它没有任何格式。
document.write("<div>");
document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
document.write("</div>");
我只想在主要部分中写一个 div,然后将所有内容写入其中,但我需要为 xml 中的每个条目创建一个,这是我能想到的唯一方法。
这是我需要为每个产品设置的 div 的基本轮廓
<div class="new_prod_box">
<a href="details.html">product name</a>
<div class="new_prod_bg"
<a href="details.html"><img src="images/thumb1.gif" alt="" title="" class="thumb" border="0" /></a>
</div>
</div>
非常感谢任何帮助。
最佳答案
这是因为你的字符串包含双引号。您需要使用单引号或转义双引号。这是无效的:
document.write("<div class="new_prod_box">");
它们是:
document.write('<div class="new_prod_box">');
document.write("<div class=\"new_prod_box\">");
关于javascript - 使用 document.write() 编写带有类的 <div>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32096486/