gpt4 book ai didi

QR Image Not Shown JS(QR图像未显示JS)

转载 作者:bug小助手 更新时间:2023-10-28 10:18:33 27 4
gpt4 key购买 nike



I am working on a QR code generator project. UI is ready, but regarding the functionality when I press the button, it does not show the QR image.

我正在做一个二维码生成器项目。用户界面已经准备好了,但是当我按下按钮时,它没有显示QR图像。


I checked the console, there were no errors. Also, I actually saw QR image once or twice, but they disappeared in less than second when I pressed the button. How can I solve this issue?

我检查了控制台,没有错误。此外,我实际上看到过一两次QR图像,但当我按下按钮时,它们在不到一秒钟的时间内就消失了。我怎么才能解决这个问题呢?


HTML /JS code:

Html/JS代码:


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>QR Code Generator</title>

<link rel="stylesheet" href="style.css" />
</head>

<body>
<div class="wrapper">
<h1>QR Code Generator</h1>
<div class="form">
<form>
<input type="text" id="qrText" name="url" placeholder="Enter URL" />
<input id="size" type="number" name="size" placeholder="Size (px)" />
<div id="imgBox">
<img src="" id="qrImage" />
</div>
<button id="btn" onclick="generateQR()">Generate QR Code</button>
</form>
</div>
</div>

<script>
let imgBox = document.getElementById("imgBox");
let qrImage = document.getElementById("qrImage");
let qrText = document.getElementById("qrText");
let qrSize = document.getElementById("size");
let btn = document.getElementById("btn");

function generateQR() {
qrImage.src =
"https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=" +
qrText.value;
imgBox.classList.add("show-img");
if (!qrText.value || qrText.value === "") {
alert("Please Enter URL");
}
}

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

CSS code:

Css代码:


* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

body {
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
font-family: Arial, sans-serif;
background: rgb(255, 179, 64);
}

.wrapper {
background: whitesmoke;
max-width: 700px;
border-radius: 10px;
padding: 20px 30px;
}

.wrapper h1{
margin-bottom: 10px;
}

.form :where(input, button){
width: 100%;
height:50px;
border-radius: 10px;
padding: 5px;
margin: 5px;;
}

.form :where(button){
background: brown;
color: wheat;
font-size: large;
border-style: none;
}

#imgBox{
width: 300px;
border-radius: 5px;
max-height: 0;
overflow: hidden;

}

#imgBox img{
width: 100%;
}
#imgBox.show-img{
max-height: 300px;
}

更多回答

<button id="btn" onclick="generateQR()" type="button">Copy Me</button> add type = button in this button element and your problem will be solved.

在这个按钮元素中添加type=Button,您的问题就解决了。

Issue is solved, thx for solution

问题解决了,谢谢解决

优秀答案推荐

The problem is that your button refreshes the page onclick, which is a default behavior since you didn't specify the type attribute.

问题是您的按钮在单击时刷新页面,这是一个默认行为,因为您没有指定type属性。


When you don't specify the type attribute, it falls by default to type="submit" which refreshes the page upon click (after submitting the data; which is not what you're doing here).

当您没有指定type属性时,它缺省为type=“Submit”,它在单击时刷新页面(在提交数据之后;这不是您在这里做的事情)。


Adding an attribute type="button" to your button element will solve the problem.

向按钮元素添加一个属性type=“Button”就可以解决这个问题。


更多回答

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com