我正在尝试随机显示5个元素中的1个。
如果我有以下内容:
<div class='random-houses'>
<img src="/assets/images/houses/1.png" data-number='1'>
<img src="/assets/images/houses/2.png" data-number='2'>
<img src="/assets/images/houses/3.png" data-number='3'>
<img src="/assets/images/houses/4.png" data-number='4'>
<img src="/assets/images/houses/5.png" data-number='5'>
<img src="/assets/images/houses/6.png" data-number='6'>
<img src="/assets/images/houses/7.png" data-number='7'>
<img src="/assets/images/houses/8.png" data-number='8'>
<img src="/assets/images/houses/9.png" data-number='9'>
<img src="/assets/images/houses/10.png" data-number='10'>
<img src="/assets/images/houses/11.png" data-number='11'>
<img src="/assets/images/houses/12.png" data-number='12'>
<img src="/assets/images/houses/13.png" data-number='13'>
<img src="/assets/images/houses/14.png" data-number='14'>
<img src="/assets/images/houses/15.png" data-number='15'>
<img src="/assets/images/houses/16.png" data-number='16'>
</div>
如何使用ember组件随机选择其中一个?我只是将它包装在逻辑的didInsertElement
块中,还是有更优雅的方法呢?我真的很感激一个有效的例子。谢谢:))
以下是我现在的表现:
export default Ember.Component.extend({
classNames: ['random-house'],
attributeBindings: ['style'],
didInsertElement: function() {
var number = Math.ceil(Math.random() * 10);
$("[data-number='" + number + "']").show();
}
});
会有更优雅的方式吗?
答案 0 :(得分:1)
我刚做了肩部手术,所以我不能打一吨/只有一只手。所以它有点难看和原始,但它应该让你开始。