我创建了一个收藏页面,允许用户保存他们选择的任何项目。每次加载/刷新此页面时,项目和按钮都是随机的。一旦洗牌/随机化,是否有办法将项目与相关按钮匹配?以下是我所做的快速示例的链接。删除JS中的评论,以便随机播放' shuffle'图像和按钮。
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>
&#13;
答案 0 :(得分:1)
这是一个工作代码段
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);
&#13;
<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>
&#13;
答案 1 :(得分:0)
我注意到您在按钮中添加了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;