jquery点击按钮弹出图片

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="js/jquery-3.4.1.min.js"></script>
<title>Document</title>

<style>
.bor{
border: 2px solid red;
width: 400px;
height: 200px;
text-align: center;
}
#main {
margin-top: 50px;
border: 1px solid black;
cursor: pointer;
padding: 30px;
}

#qrcode img {
position: absolute;
top: 50%;
left: 40%;
width: 300px;
height: 300px;
display: block;
}
#qrcode {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(255, 255, 255, 0.6);
z-index: 9999;
display: none;
}
</style>
</head>
<body>
<div class="bor">
<button class="main">点击此处 弹出你想要的</button>
<div class="qrcode">
<img class="image"  width="100%" height="100%" src="img/12.png">
</div>
</div>
<div class="bor">
<button class="main">点击此处 弹出你想要的</button>
<div class="qrcode">
<img class="image"  width="100%" height="100%" src="img/12.png">
</div>
</div>

<script>
//当本页面有两个按钮点击事件的时候,不适合用id(因为id是唯一的),需要把id换为class,最为简单的修改方法;
// 这样才会是实现你想要的效果,点击按钮弹出图片

$(function() {
$(".main").click(function() {
$(".qrcode").fadeIn("slow");
});
$(".qrcode").click(function() {
$(".qrcode").fadeOut("slow");
})
});

 //该段代码适合本页面只有一个按钮点击
$(function() {
$("#main").click(function() {
$("#qrcode").fadeIn("slow");
});
$("#qrcode").click(function() {
$("#qrcode").fadeOut("slow");
})
});

</script>
</body>
</html>

以上是运用jquery里面的fade()函数写的点击事件。

 

下面这个是用传统的onclick写出来的,有一点点小问题,刚开始是有效的,今天重新写出来之后不知道为什么没效果了,大家可以帮我看下,我也发布出来

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="js/jquery-3.4.1.min.js"></script>
<title>Document</title>

<style>
.bor{
border: 2px solid red;
width: 400px;
height: 200px;
text-align: center;
}
#main {
margin-top: 50px;
border: 1px solid black;
cursor: pointer;
padding: 30px;
}

扫描二维码关注公众号,回复: 7366872 查看本文章

#qrcode img {
position: absolute;
top: 50%;
left: 40%;
width: 300px;
height: 300px;
display: block;
}
#qrcode {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(255, 255, 255, 0.6);
z-index: 9999;
display: none;
}
</style>
</head>
<body>
<div class="bor">
<button id="main">点击此处 弹出你想要的</button>
<div id="qrcode">
<img id="image" onclick="test(this)" width="100%" height="100%" src="img/12.png">
</div>
</div>
<div class="bor">
<button id="main">点击此处 弹出你想要的</button>
<div id="qrcode">
<img id="image" onclick="test(this)" width="100%" height="100%" src="img/12.png">
</div>
</div>

<script>

// 传统的方法来写,onclick

function test(obj){
console.log("11111")
$(obj).next().fadeIn("slow");
$(obj).next().click(function() {
$(this).fadeOut("slow");
})
var hh = $(obj).next().html();
console.log(hh)
}

</script>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/qiaomeier/p/11589951.html