我当前正在尝试从数据库中检索记录并 append 每条记录,以便可以显示它们。
我发现在该特定行( append 图像的部分)中的某个位置存在语法错误。我对如何正确 append 图像作为链接一无所知,可以单击该链接以传递参数 arr[i]itemID 进入下一页。
$("#wallcontentset").append("<b>" + arr[i].categoryName + "</b><br/>" + arr[i].itemName + "<br/>" + arr[i].price + "<br/>" + arr[i].soldStatus + "<br/>" + "<a href='#' onclick="window.location='viewtheitem.html?itemID=" + arr[i].itemID + "'"> + <img src='" + serverURL() + "/images/" + arr[i].imagefile + "'height='150'></a><br/><hr>");
有人可以帮我吗?
我将其更改为下面显示的代码,但单击 append 和显示的图像时没有任何反应:
enter code here $("#wallcontentset").append("<b>" + arr[i].categoryName + "</b><br/>" + arr[i].itemName + "<br/>" + arr[i].price + "<br/>" + arr[i].soldStatus + "<br/>" + "<a href='#' onclick=\"window.location='viewtheitem.html?itemID=" + arr[i].itemID + "\"><img src='" + serverURL() + "/images/" + arr[i].imagefile + "'height='150'></a><br/><hr>");
最佳答案
如果您查看代码末尾,您会发现存在引号问题
+ "<a href='#' onclick="window.location='viewtheitem.html?itemID=" + arr[i].itemID + "'"> + <img src='" + serverURL() + "/images/" + arr[i].imagefile + "'height='150'></a><br/><hr>");
你用 "开始一个新字符串,但在 onclick 中使用相同的符号,这样你就结束了你的字符串。所以你要么必须使用 ' 而不是 ",要么转义引号。 因此,实现此目的的一种方法是:
+ "<a href='#' onclick=\"window.location='viewtheitem.html?itemID=" + arr[i].itemID + "\"> + <img src='" + serverURL() + "/images/" + arr[i].imagefile + "'height='150'></a><br/><hr>");
希望这能解决您的问题。
关于javascript - Onclick window.location 并传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35238558/