javascript - 使用 document.write() 编写带有类的 <div>

标签 javascript html

我正在尝试使用脚本中的 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/

相关文章:

html - 尝试将不同的 CSS 类分配给单选按钮的输入标签

html - 透明渐变

javascript - 如何检测正则表达式是否有匹配项?

javascript - 试图涂黑可移动 div 之外的所有内容

javascript - 试图从 ("\uf2bb"中删除\u - 很棒的字体图标)。这样我就可以得到 "f2bb"

javascript - 显示元素内的多个字符

C# HttpWebRequest - 如何在不下载的情况下区分 HTML 和 XML 页面?

html - 如何显示不同的号码图像覆盖图像?

javascript - 将 seriate Node.js 与 Web 应用程序连接的最佳方法是什么

javascript - 100% 背景图 block ,100% 高度 div