您能告诉我如何在li
中随机显示两个jQuery
吗?我试过这个
$('#id_show12').click(function() {
var random1 = Math.floor(Math.random() * 7);
var random2 = Math.floor(Math.random() * 7);
$('.show12 li').not(':eq(' + random1 + '), :eq(' + random2 + ')').hide();
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="show12">
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
</ul>
</div>
<button id="id_show12">show only one two li</button>
&#13;
答案 0 :(得分:1)
您需要重置以显示所有元素,然后在其上运行您的逻辑,请参阅下面的
$('#id_show12').click(function() {
var random1 = Math.floor(Math.random() * 7);
var random2 = Math.floor(Math.random() * 7);
$('.show12 li')
.show() /* <-- reset */
.not(':eq(' + random1 + '), :eq(' + random2 + ')')
.hide();
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="show12">
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
</ul>
</div>
<button id="id_show12">show only one two li</button>
&#13;
答案 1 :(得分:1)
当你隐藏它们之前显示时,你的所有逻辑都很有效。
Class User: NSObject, NSCoding{
var userName: String
var email_id: String
var password: String
init(userName: String, email_id: String, password: String){
self.userName = userName
self.email_id = email_id
self.password = password
}
required conveniece init(coder decoder: NSCoder){
let userName = decoder.decodeObject(forKey: "userName") as? String ?? ""
let email_id = decoder.decodeObject(forKey: "email_id") as? String ?? ""
let password = decoder.decodeObject(forKey: "password") as? String ?? ""
self.init(
userName=userName,
email_id=email_id,
password=password
)
}
func encode(with coder: NSCoder){
coder.encode(self.userName, forKet="userName")
coder.encode(self.email_id, forKet="email_id")
coder.encode(self.password, forKet="password")
}
}
目前它无法正常工作,因为您隐藏它们并且再次单击时不会显示它们。
请注意,当随机数相同时,您不会处理此案例。 (0,0 | 3,3)如果你不处理这个案子,有时它只会显示一个选项。
答案 2 :(得分:0)
js code:
$('#id_show12').click(function() {
var random1= Math.floor(Math.random() * 7);
var random2= Math.floor(Math.random() * 7);
if(random1 == random2){
if(random1 == 6){
random1= 5;
}
if(random1 = 0){
random1= 1;
}
else{
random1=random1+1;
}
}
$( "li" ).each(function( index ) {
if(random1 != index & random2 != index){
$( this ).hide();
}
else{
$( this ).show();
}
});
})