douruyun8153 2014-02-14 10:38
浏览 21
已采纳

仅显示Array中的一个项目

With the following code, when you click on a hyperlink, it loads array items into the brand-content div. When you click the Nike hyperlink for example, it only loads item 3 from the array and the items 1 and 2 are missing?

 <?php
 $brand = array (
 "nike" => array (
            array('logo'=>'images/nike.png', 'description' =>'some nike text1'),
            array('logo'=>'images/nike.png', 'description' =>'some nike text2'),
            array('logo'=>'images/nike.png', 'description' =>'some nike text3')),
 "puma" => array(
            array('logo'=>'images/puma.png', 'description' =>'some puma text'),
            array('logo'=>'images/puma.png', 'description' =>'some puma text'),
            array('logo'=>'images/puma.png', 'description' =>'some puma text')),
 "addidas" => array(
            array('logo'=>'images/addidas.png', 'description'=>'some addidas text1'),
            array('logo'=>'images/addidas.png', 'description'=>'some addidas text2'),
            array('logo'=>'images/addidas.png', 'description'=>'some addidas text3')),
 "Asics" => array(
            array('logo'=>'images/Asics.png', 'description'=>'some Asics text'),
            array('logo'=>'images/Asics.png', 'description'=>'some Asics text'),
            array('logo'=>'images/Asics.png', 'description'=>'some Asics text')),
 "Qicksilver" => array(
            array('logo'=>'images/Qicksilver.png', 'description'=>'some Qicksilver text'),
            array('logo'=>'images/Qicksilver.png', 'description'=>'some Qicksilver text'),
            array('logo'=>'images/Qicksilver.png', 'description'=>'some Qicksilver text')),
  );
  ?>

<style type="text/css">
    a {
        margin-left: 10px;
    }
    #brand-content {
        margin-top: 50px;
        border: solid 1px #000;
        padding: 10px;
        display: none;
        width: 50%;
    }
    #brand-content img {
        margin-right: 25px;
    }

</style>

<script>var brand =<?php echo json_encode($brand) ?>;// json_encode($brand) is equivalent to the following:      {"nike":[{"logo":"images\/nike.png","description":"some nike text"},     {"logo":"images\/nike.png","description":"some nike text"},{"logo":"images\/nike.png","description":"some nike text"}],"puma":[{"logo":"images\/puma.png","description":"some puma text"},  {"logo":"images\/puma.png","description":"some puma text"},{"logo":"images\/puma.png","description":"some puma text"}],"addidas":[{"logo":"images\/addidas.png","description":"some addidas text"},{"logo":"images\/addidas.png","description":"some addidas text"},{"logo":"images\/addidas.png","description":"some addidas text"}],"Asics":[{"logo":"images\/Asics.png","description":"some Asics text"},{"logo":"images\/Asics.png","description":"some Asics text"},{"logo":"images\/Asics.png","description":"some Asics text"}],"Qicksilver":[{"logo":"images\/Qicksilver.png","description":"some Qicksilver text"},{"logo":"images\/Qicksilver.png","description":"some Qicksilver text"},  {"logo":"images\/Qicksilver.png","description":"some Qicksilver text"}]}

    function readyLinks() {
        for (var n in brand) {
            for (var i in brand[n]) {
                var link = document.createElement("a");
                link.href = brand[n][i].logo;
                link.innerHTML = n;
                link.setAttribute("data-description", brand[n][i].description);
                link.onclick = function() {
                    document.getElementById("brand-content").innerHTML = "<img src=\"" + this.href + "\"/>" + this.getAttribute("data-description");
                    document.getElementById("brand-content").style.display = "block";
                    return false;
                }
            }
            document.getElementById("brand-nav").appendChild(link);
        }
    }


    window.onload = function() {
        readyLinks();
    }

</script>

<!-- body -->

<div id="brand-nav"></div>

<div id="brand-content"></div>

The actual array that I will be using will be like the following:

  "Nike" => array( 
        array('id'=>'01','logo'=>'images/nike-logo.jpg', 'productTitle' => "Nike Air   Max",'productImage' => "images/airmax.jpg",'productDescription' => "Nike Air Max are awesome ",'rrp' => "RRP £100.00",'salePrice' => "Now ONLY £80.00"), 

  array('id'=>'02','logo'=>'images/nike-logo.jpg', 'productTitle' => "Nike Plus Running Shoes",'productImage' => "images/plus.jpg",'productDescription' => "Nike plus are even more awesome ",'rrp'  => "RRP £120.00",'salePrice' => "Now ONLY £90.00"),  

So do I need to add additional getAttributes for the additional fields in the array for everything to display?

this.getAttribute("id");
this.getAttribute("productTitle");
this.getAttribute("productDescription");

Volterony

  • 写回答

2条回答 默认 最新

  • dsfdgdsfd23212 2014-02-14 11:18
    关注
    function readyLinks()
     {
     for (var n in brand){
     for (var i in brand[n]){
     var link = document.createElement("a");
     link.href = brand[n][i].logo;
     link.innerHTML = n;
     link.setAttribute("data-description",brand[n][i].description);
    
     link.onclick = function(){
     document.getElementById("brand-content").innerHTML = "<img src=\""+this.href+"\"/>" +  this.getAttribute("data-description");
     document.getElementById("brand-content").style.display = "block";
     return false;}
     document.getElementById("brand-nav").appendChild(link);
     }}}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败