Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 奇怪的无效或意外令牌错误_Javascript - Fatal编程技术网

Javascript 奇怪的无效或意外令牌错误

Javascript 奇怪的无效或意外令牌错误,javascript,Javascript,我有以下几句话: document.write('<td><input value="Add to ShopBakset" type="button" onClick="addToBasket(\'' + JSON.stringify(products[i]) + '\')"/></td>'); document.write(“”); 以及以下代码: <!DOCTYPE HTML> <html> <he

我有以下几句话:

document.write('<td><input value="Add to ShopBakset" 
        type="button" onClick="addToBasket(\'' + JSON.stringify(products[i]) + '\')"/></td>');
document.write(“”);
以及以下代码:

 <!DOCTYPE HTML>
 <html>
 <head>
 <link rel="stylesheet" href="styles.css">
 </head>
 <body>
 <div class="center">
 <h1 align="center">Shop Basket</h2>
 <script type="text/javascript" src="external.js"></script>

 <table align="center">
<tr>
    <th align="left">Price</th>
    <th>Product</th>
    <th></th>
</tr>
<script>
for(let i=0;i<products.length;i++){
    for(let key in products[i]){
        document.write("<tr>");
        document.write("<td>" + key + "</td>");
        document.write("<td>" + products[i][key] + "</td>");
        document.write('<td><input value="Add to ShopBakset" 
    type="button" onClick="addToBasket(\'' + JSON.stringify(products[i]) + '\')"/></td>');
        document.write("</tr>");
    }
}
</script> // an error occurs here
    </table> 
    <center>
<a href="html-link.htm"><img src="shopbasket.jpg" title="basket" 
    alt="basket"></a>
    </center>
    </div>
    <p id="change"></p>
    </body>
    </html>

购物篮
价格
产品

对于(设i=0;i,问题是不能有带有
'
'
的多行字符串。应使用
'
关闭字符串。并将
+
与新字符串一起添加

document.write('<td><input value="Add to ShopBakset"'+ 
        'type="button" onClick="addToBasket(\'' + JSON.stringify(products[i]) + '\')"/></td>');
document.write(“”);
更好的方法是使用

document.write(`
`) 

JS字符串没有在新行上传播。
document.write
将删除所有其他html标记用简单的话来说是什么意思?你认为我应该将其拆分为3个字符串吗?因为还是同一个错误吗?@graluc是的,这是由于拆分为三行。@graluc每次将字符串拆分为新行时。只需将字符串编码即可g,并在该行后面使用
+
,然后从新行开始一个新字符串。
document.write('<td><input value="Add to ShopBakset"'+ 
        'type="button" onClick="addToBasket(\'' + JSON.stringify(products[i]) + '\')"/></td>');
document.write(`<td><input value="Add to ShopBakset"
            type="button" onClick="addToBasket(${JSON.stringify(products[i])}')"/>
            </td>`)