javascript代码未创建cookie的问题



下面的代码来自一个较旧的html文档,并且有效。它调用一个脚本来创建一个cookie,另一个文档使用该cookie来显示图像[imagetag]及其描述[imagetext]。

<script>
document.write("<a href='albumview-a.html'><img onclick="(createCookie('albumcookie','" + count[1] + "',0))"" + imagetag[1] + " alt='' border='1' width='300'></a>");
document.write("<p class='noindent'>" + imagetext[1] + "</p>");
</script>

然而,我想更改的是,它只显示描述。当我更改下面显示的代码时,它无法编写cookie,我可怜的大脑无法找出原因。有什么想法吗?

<script>
document.write("<a href='albumview-a.html'><onclick="(createCookie('albumcookie','" + count[1] + "',0))"" + "<p class='noindent'>" + imagetext[1] + "</p></a>");
</script>

因为您生成的HTML是无效的

查看

const count = ["1","2"]
const imagetag = ["...","src=https://assets.stickpng.com/images/58afd6b70187e59a7d8a8f1c.png"]
const imagetext = ["Text 1","Text 2"]
document.write("<a href='albumview-a.html'><img onclick="(createCookie('albumcookie','" + count[1] + "',0))"" + imagetag[1] + " alt='' border='1' width='300'></a>");
document.write("<p class='noindent'>" + imagetext[1] + "</p>");

const imagetext = ["Text 1","Text 2"]
const count = ["1","2"]
document.write("<p class='noindent'><a href='albumview-a.html' onclick="(createCookie('albumcookie','" + count[1] + "',0))">" + imagetext[1] + "</a></p>");

最新更新