Writing a <div> with a class using document.write()
我正在尝试使用脚本中的document.write()来编写div。我似乎能够编写一个空白的div,但是每当我向div中添加类或任何属性时,它就不再写任何内容。
这是我要运行的代码。
1 2 3 4 5 6 7 8 | document.write(""); document.write(""); document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue); document.write(""); document.write(""); document.write("<img src="images/thumb1.gif" alt="" title="" class="thumb" border="0" />"); document.write(""); document.write(""); |
我已经测试了xml,因此在加载xml时没有问题,并且我在要编写该脚本的部分中运行了一个脚本,并且该脚本可以在该区域中很好地写入。我也使用普通的div进行了测试,它可以正常工作,将类分配给该div时似乎只是一个问题。
我已经运行了这段代码,以查看document.write()写入div标签是否存在问题,并且可以正常工作,但是它没有任何格式。
1 2 3 | document.write(""); document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue); document.write(""); |
我只需要在主要部分中编写一个div,然后将所有内容写入该div中,但是我需要为xml中的每个条目创建一个div,这是我想到的唯一方法。
这是我需要为每个产品划分的div的基本轮廓
1 2 3 | product name <div class="new_prod_bg" <img src="images/thumb1.gif" alt="" title="" class="thumb" border="0" /> |
非常感谢您的帮助。
这是因为您的字符串包含双引号。您需要使用单引号,或转义双引号。这是无效的:
1 | document.write(""); |
这些是:
1 2 | document.write(''); document.write(""); |
尝试使用单引号,并且只写一次:
1 2 3 4 5 6 7 8 9 | var myhtml =""; myhtml +=""); myhtml += x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue; myhtml +=""; myhtml +=""; myhtml +="<img src='images/thumb1.gif' alt='' title='' class='thumb' border='0' />"; myhtml +=""; myhtml +=""; document.write(myhtml); |
尝试一下:
1 2 3 4 5 6 | document.write('\\ ' + x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue + '\\ \\ <img src="images/thumb1.gif" alt="" title="" class="thumb" border="0" />\\ \\ '); |
您正在弄乱代码中的
被替换
在您的HTML中也有div开始