87,904
社区成员
发帖
与我相关
我的任务
分享
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>一键打开网站</title>
<style>
* {
margin: 0px;
padding: 0px;
}
html, body {
height: 100%;
font-size: 12px;
}
.textarea-box {
padding: 20px;
}
.textarea-box #textarea {
width: 100%;
}
.footer {
text-align: center;
}
.footer button {
display: inline-block;
height: 30px;
line-height: 30px;
width: 100px;
}
</style>
</head>
<body>
<div class="textarea-box">
<textarea id="textarea" rows="10">
https://element.eleme.cn/2.4/#/zh-CN/component/selecthttp://fanyi.youdao.com/https://www.baidu.com/
</textarea>
</div>
<div class="footer">
<button id="openBtn">
打开网站
</button>
<button id="clearBtn">
清空
</button>
</div>
<script>
document.body.onload = function() {
var textareaDom = document.querySelector("#textarea"),
openBtnDom = document.querySelector("#openBtn"),
clearBtnDom = document.querySelector("#clearBtn");
openBtnDom.onclick = function() {
if (textareaDom) {
fnOpenUrl(textareaDom.value || "");
}
}
clearBtnDom.onclick = function() {
if (textareaDom) {
textareaDom.value = "";
}
}
function fnOpenUrl(str) {
if (str) {
(str.split("http") || []).forEach(url => {
url = url.replace(/\s/g, "");
if (url) {
url = url && "http" + url;
if (/(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/.test(url)) {
window.open(url);
} else {
console.log("不是一个网址", url)
}
}
});
}
}
}
</script>
</body>
</html>