for 循环中的 onClick 未触发
下面是来自最近的计算机艺术杂志教程的代码。我试图做的是在循环中实现一个 onClick
函数,以便每个 div
都可以点击并加载相应的网页。
我有一个数组,目前只有一组值,但您可以了解多个相似值的想法:
var itemsHTML = '',
items = [
{
itemType:'ani',
itemName:'Show Reel 10-11',
path:'thumb_showreel.jpg',
urlPath:'showreel.html'
}];
以及从数组中获取信息的循环(请注意 onClick
-这是我的输入,其他所有内容都来自教程:
for(var i = 0; i < items.length; i++) {
var type = items[i]['itemType'];
var name = items[i]['itemName'];
var path = items[i]['path'];
var urlPath = items[i]['urlPath'];
itemsHTML += " <div class=\"element " + type + "\" style=\"background-image:url('portfolio/" + type + "/" + path + "')\" onClick=\"document.location='http://www.thepapertorium.co.uk/'" + urlPath + "\">";
itemsHTML += " <div class=\"label\">";
itemsHTML += " <h1>" + name + "<\/h1>";
itemsHTML += " <\/div>";
itemsHTML += " <\/div>";
}
同样的 onClick
取自我的标题横幅,它在那里工作正常,所以我想我可以将其粘贴到此处,并且自然地粘贴它。到 for
循环的语法需要,但我没有运气,请帮助解决这个简单的困境!
What I have below is code from a recent tutorial of Computer Arts magazine. What I have tried to do is implement an onClick
function within the loop so that each div
is clickable and will load the corresponding webpage.
I have an array, currently with only one set of values but you can get the idea of multiple values that are similar:
var itemsHTML = '',
items = [
{
itemType:'ani',
itemName:'Show Reel 10-11',
path:'thumb_showreel.jpg',
urlPath:'showreel.html'
}];
And the loop that takes the info from the array (Please take note of the onClick
- this is my input, everything else is from the tutorial:
for(var i = 0; i < items.length; i++) {
var type = items[i]['itemType'];
var name = items[i]['itemName'];
var path = items[i]['path'];
var urlPath = items[i]['urlPath'];
itemsHTML += " <div class=\"element " + type + "\" style=\"background-image:url('portfolio/" + type + "/" + path + "')\" onClick=\"document.location='http://www.thepapertorium.co.uk/'" + urlPath + "\">";
itemsHTML += " <div class=\"label\">";
itemsHTML += " <h1>" + name + "<\/h1>";
itemsHTML += " <\/div>";
itemsHTML += " <\/div>";
}
This same onClick
was taken from my header's banner. It works correctly there, so I figured I could paste it into here, and, naturally, adhere it to the syntax that the for
loop requires, but I have had no luck. Please help with this simple dilemma!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的问题可能出在单个
'
中 - 它应该位于 URL 的末尾:onClick=\"document.location='http://www.thepapertorium.co.uk/" + urlPath + "'\"
Your problem may be in single
'
- it should go to the end of URL:onClick=\"document.location='http://www.thepapertorium.co.uk/" + urlPath + "'\"
我认为您的问题是
document.location
而不是window.location
。但我不会为此使用 javascript,您可以使用锚标记来代替:I think your problem is
document.location
instead ofwindow.location
. But I wouldn't use javascript for that, you could use an anchor tag instead:引号问题 - 我通过反转 " 和 ' 为您简化了它们,
我还添加了 ; 光标:指针并将 document.location 更改为 window.location,因为这更正确。实际上 bfavaretto 确实是正确的,您不需要 javascript更改页面 - 链接甚至会免费为您提供指针
DEMO
下次尝试jsfiddle.net 和 firebug
Quotes issue - I simplified them for you by reversing the " and '
I also added ; cursor:pointer and changed document.location to window.location since that is more correct. Actually bfavaretto is indeed correct in saying you do not need a javascript to change the page - a link will even give you the pointer for free
DEMO
Next time try jsfiddle.net and firebug