筛选出有邮箱的li项;
用正则表达式以及exec()函数进行匹配。
详细代码如下
<!DOCTYPE html>
<html> <head> <Meta charset="utf-8"> <Meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>Examples</title> <Meta name="description" content=""> <Meta name="keywords" content=""> <link href="" rel="stylesheet"> <style type="text/css"> ul{ border: 1px dotted black; background: green; border-radius: 10px; width: 400px; height: 200px; } li{ list-style: none; font-family: "微软雅黑"; } </style> <script type="text/javascript"> /* window.onload=function(){ var lis=document.getElementsByTagName("li"); for(var i=0;i<lis.length;i++){ lis[i].onclick=function(num){ return function(){ return alert(num); } }(i) } } function liuzhe(){ this.bark=function(){ alert("最喜欢刘哲"); } } liuzhe.xiaozhe=function(){ alert("小哲喜欢你"); } var zhe=new liuzhe(); console.log(zhe) console.log(liuzhe.xiaozhe) zhe.bark(); liuzhe.xiaozhe(); */ function fn(){ var lz=document.getElementsByTagName("li"); var patt=/\w+@\w+(\.+\w)+/g; for(var i=0;i<lz.length;i++){ if(patt.exec(lz[i].innerHTML)!==null){ lz[i].style.background="red"; lz[i].style.color="yellow"; } } } </script> </head> <body> <button type="text" onclick="fn()">查找邮箱</button> <ul> <li>喜欢你小哲<liuzhe@qq.com></li> <li>喜欢你小哲</li> <li>喜欢你小哲<liuzhe@qq.baidu.com></li> <li>喜欢你小哲</li> </ul> </body> </html> 原文链接:https://www.f2er.com/regex/360536.html