实时获取文本框1的链接并输出到文本框2
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>网页名称</title>
<style>
</style>
</head>
<body>
<input type="text" id="inputBox1" oninput="checkForLink()">
<input type="text" id="inputBox2">
<script> function checkForLink() {
var input = document.getElementById("inputBox1").value;
var linkRegex = /https?:\/\/[^\s]+/g; // 正则表达式匹配链接
var match = linkRegex.exec(input);
if (match) {
var link = match[0];
document.getElementById("inputBox2").value = link; // 将链接转移到文本框2
}
}
</script>
</body>
</html>
清除文本框1的链接
function checkForLink() {
var input = document.getElementById("inputBox1").value;
var linkRegex = /https?:\/\/[^\s]+/g; // 正则表达式匹配链接
var matches = input.match(linkRegex);
if (matches) {
for (var i = 0; i < matches.length; i++) {
var link = matches[i];
document.getElementById("inputBox2").value += link + " "; // 将链接追加到文本框2
}
input = input.replace(linkRegex, ""); // 删除文本框1中的链接
document.getElementById("inputBox1").value = input;
}
}