【问题标题】:matching relevant button to image将相关按钮与图像匹配
【发布时间】:2016-01-15 01:26:58
【问题描述】:

我创建了一个收藏夹页面,允许用户保存他们选择的任何项目。每次此页面加载/刷新时,项目和按钮都是随机的。一旦洗牌/随机化,有没有办法将项目与相关按钮匹配?下面是我制作的一个快速示例的链接。从 JS 中删除 cmets 以“洗牌”图像和按钮。

https://jsfiddle.net/gu7ccv5d/2/

/* var gridOnHomePage = document.querySelector(".grid"), // get the list
    temp = gridOnHomePage.cloneNode(true), // clone the list
    i = temp.children.length + 1;

// shuffle the cloned list (better performance)
while( i-- > 0 )
    temp.appendChild( temp.children[Math.random() * i |0] );

gridOnHomePage.parentNode.replaceChild(temp, gridOnHomePage); */
<div class="grid">
  <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/43/Red_flag.svg/2000px-Red_flag.svg.png" width="50" height="50" />
  <button type="button" class="addToFavorites" color="red" img="https://upload.wikimedia.org/wikipedia/commons/thumb/4/43/Red_flag.svg/2000px-Red_flag.svg.png">
    Add Red
  </button>
  <img src="http://epochmod.com/forum/uploads/monthly_2015_09/2000px-Solid_blue.svg.png.09e2d6f1de3f9a9192388a1737c19280.png" width="50" height="50" />
  <button type="button" class="addToFavorites" color="blue" img="http://epochmod.com/forum/uploads/monthly_2015_09/2000px-Solid_blue.svg.png.09e2d6f1de3f9a9192388a1737c19280.png">
    Add Blue
  </button>
  <img src="http://greensportsalliance.org/images/darkGreenSquare.gif" width="50" height="50" />
  <button type="button" class="addToFavorites" color="green" img="http://greensportsalliance.org/images/darkGreenSquare.gif">
    Add Green
  </button>
</div>

【问题讨论】:

    标签: javascript jquery shuffle


    【解决方案1】:

    这是一个有效的 sn-p

    var gridOnHomePage = document.querySelector(".grid"), // get the list
        temp = gridOnHomePage.cloneNode(true), // clone the list
        i = temp.children.length + 1;
    
    // shuffle the cloned list (better performance)
    while( i-- > 0 )
        temp.appendChild( temp.children[Math.random() * i |0] );
    
    gridOnHomePage.parentNode.replaceChild(temp, gridOnHomePage);
    <div class="grid">
    <span>
      <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/43/Red_flag.svg/2000px-Red_flag.svg.png" width="50" height="50" />
      <button type="button" class="addToFavorites" color="red" img="https://upload.wikimedia.org/wikipedia/commons/thumb/4/43/Red_flag.svg/2000px-Red_flag.svg.png" >Add Red
      </button>
    </span>
    
    <span>
      <img src="http://epochmod.com/forum/uploads/monthly_2015_09/2000px-Solid_blue.svg.png.09e2d6f1de3f9a9192388a1737c19280.png" width="50" height="50" />
      <button type="button" class="addToFavorites" color="blue" img="http://epochmod.com/forum/uploads/monthly_2015_09/2000px-Solid_blue.svg.png.09e2d6f1de3f9a9192388a1737c19280.png" >Add Blue 
      </button>
    </span>
    <span>
     <img src="http://greensportsalliance.org/images/darkGreenSquare.gif" width="50" height="50" />
      <button type="button" class="addToFavorites" color="green" img="http://greensportsalliance.org/images/darkGreenSquare.gif" >Add Green
      </button>
    </span>
    </div>

    【讨论】:

    • 如果我在图像周围使用图形标签会有什么建议?当我尝试使用图形标签时会破坏内容流
    • 为你的图形标签设置样式,例如“ figure { display:inline-block; width:whatever; height:whatever; margin:auto;} " ..应该可以工作..
    • 如果这确实解决了问题,请勾选答案为解决...D
    【解决方案2】:

    我注意到您在按钮中添加了 img 属性:

    <button type="button" class="addToFavorites" color="blue" img="http://epochmod.com/forum/uploads/monthly_2015_09/2000px-Solid_blue.svg.png.09e2d6f1de3f9a9192388a1737c19280.png">
    

    您可以改用数据属性,如下所述:

    https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_data_attributes

    您的代码如下所示:

    <button type="button" class="addToFavorites" color="blue" data-img="http://epochmod.com/forum/uploads/monthly_2015_09/2000px-Solid_blue.svg.png.09e2d6f1de3f9a9192388a1737c19280.png">
    

    你可以像这样访问它:

    yourButton = document.getElementsByClassName("addToFavorites");
    imageDataAttribute = yourButton.dataset.img;
    

    【讨论】:

    • 好的,谢谢。我会看看并试一试。我对 jQuery 还是很陌生,所以在使用 yourButton JS 时,我需要将它包含在我的“shuffle”函数中还是单独包含?
    • 不,我只回答了如何将图像 url 属性添加到按钮以及如何访问它。您必须将其添加到您的随机播放代码中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多