javascript - 如何在jQuery中的点击功能上使用else if条件?
问题描述
我正在制作一个卢多游戏,所以如果我单击 4 个框中的任何一个框(例如r1
、r2
、r3
、r4
点击)。我尝试else if
了条件,但它不起作用。请帮帮我。
这里r1
,r2
和是s'r3
的盒子。r4
id
我想做的事情:
if ($('#r1').click()) {
$('#r1').click(function() {
$(this).css("position", "absolute");
$(this).animate((P[0]), 1000);
});
} else if ($('#r2').click()) {
$('#r2').click(function() {
$(this).css("position", "absolute");
$(this).animate((P[0]), 1000);
});
} else if ($('#r3').click()) {
$('#r3').click(function() {
$(this).css("position", "absolute");
$(this).animate((P[0]), 1000);
});
} else if ($('#r4').click()) {
$('#r2').click(function() {
$(this).css("position", "absolute");
$(this).animate((P[0]), 1000);
});
}
解决方案
$(".btn").on("click",function(){
var _ID=this.id;
if(_ID=="#r1")
$(this).css("background-color","red");
else if(_ID=="#r2")
$(this).css("background-color","yellow");
else if(_ID=="#r3")
$(this).css("background-color","green");
else if(_ID=="#r4")
$(this).css("background-color","blue");
console.log(_ID);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button type="button" id="#r1" class="btn">click1</button>
<button type="button" id="#r2" class="btn">click2</button>
<button type="button" id="#r3" class="btn">click3</button>
<button type="button" id="#r4" class="btn">click4</button>
推荐阅读
- excel - Excel CUBEVALUE & CUBESET 计数记录大于数字
- javascript - 如何在 nodemailer 中正确发送电子邮件并关闭 smtp 连接?
- php - Laravel如何在两个表中进行多个文件上传和存储数据
- node.js - 如何根据内部值的值单击div外部?
- android - 在片段中保留对视图的引用会导致内存泄漏?
- sed - 如何转换文件(使用 SED)?
- javascript - 按对象值过滤对象数组
- java - SAXParserFactory 安全处理功能在 jboss 服务器中不起作用
- javascript - 日历显示多次
- sql - 动态投射“新”和“旧”