很多时候,我们希望用户在网页点击你的手机号码时,页面就自动跳转到微信聊天界面,以提升网站的资源拓展效率。如下代码,即可实现:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<!-- HTML 代码 -->
<p>我的手机号码是:13888888888</p>
<p>你的手机号码是:13999999999</p>
<script>
// JavaScript 代码
const phoneRegex = /(d{11})/g; // 匹配11位手机号码的正则表达式
// 查找所有包含手机号码的段落元素
const paragraphs = document.getElementsByTagName("p");
for (let i = 0; i < paragraphs.length; i++) {
const paragraph = paragraphs[i];
const phoneNumbers = paragraph.innerHTML.match(phoneRegex);
if (phoneNumbers) {
// 将手机号码替换为微信的打开链接
const wechatLink = "weixin://dl/chat?chat_type=etc&" + "xxxxxxx"; // 替换为实际的微信链接
const newContent = paragraph.innerHTML.replace(phoneRegex, `<a href="${wechatLink}">$1</a>`);
paragraph.innerHTML = newContent;
}
}
</script>
</body>
</html>