for 循环中的 onClick 未触发

发布于 2025-01-06 04:59:31 字数 1286 浏览 0 评论 0原文

下面是来自最近的计算机艺术杂志教程的代码。我试图做的是在循环中实现一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恋竹姑娘 2025-01-13 04:59:31

您的问题可能出在单个 ' 中 - 它应该位于 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 + "'\"

将军与妓 2025-01-13 04:59:31

我认为您的问题是 document.location 而不是 window.location。但我不会为此使用 javascript,您可以使用锚标记来代替:

itemsHTML += "    <a class=\"element " + type + "\"  style=\"display: block; background-image:url('portfolio/" + type + "/" + path + "')\" href=\"http://www.thepapertorium.co.uk/" + urlPath + "\">";

I think your problem is document.location instead of window.location. But I wouldn't use javascript for that, you could use an anchor tag instead:

itemsHTML += "    <a class=\"element " + type + "\"  style=\"display: block; background-image:url('portfolio/" + type + "/" + path + "')\" href=\"http://www.thepapertorium.co.uk/" + urlPath + "\">";
梦巷 2025-01-13 04:59:31

引号问题 - 我通过反转 " 和 ' 为您简化了它们,

我还添加了 ; 光标:指针并将 document.location 更改为 window.location,因为这更正确。实际上 bfavaretto 确实是正确的,您不需要 javascript更改页面 - 链接甚至会免费为您提供指针

DEMO

itemsHTML += '<div class="element ' + type + '" style="background-image:url(\'portfolio/' + type + '/' + path + '\')" onClick="window.location=\'http://www.thepapertorium.co.uk/' + urlPath + '\'">';

下次尝试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

itemsHTML += '<div class="element ' + type + '" style="background-image:url(\'portfolio/' + type + '/' + path + '\')" onClick="window.location=\'http://www.thepapertorium.co.uk/' + urlPath + '\'">';

Next time try jsfiddle.net and firebug

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文