码迷,mamicode.com
首页 > Web开发 > 详细

敏感词过滤,js封装class选择器:

时间:2017-02-25 10:59:15      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:head   class选择器   nod   inpu   round   button   regexp   bsp   cti   

敏感词过滤:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        window.onload = function () {
            var oBtn = document.getElementById("btn");
            var aT = document.getElementsByTagName("textarea");
            oBtn.onclick = function () {
                var re = /靠|SB|你妹的/ig;
                aT[1].value = aT[0].value.replace(re, function (str) {
                    var tmp = ‘‘;
                    for (var i = 0; i < str.length; i++) {
                        tmp += "*";
                    }
                    return tmp;
                });
            };
        }
    </script>
</head>
<body>
原文 <br/>
<textarea name="" id="" cols="30" rows="10"></textarea>
<br>
替换后: <br>
<textarea name="" id="" cols="30" rows="10"></textarea>
<br>
<input type="button" value="替换" id="btn">
</body>
</html>

通过class选择元素:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        div {
            padding: 20px;
            margin: 20px;
            border: 1px solid #000;
        }
    </style>
    <script>
        function getByClass(cName, obj) {
            var o = null;
            if (arguments.length == 2) {
                o = obj;
            } else {
                o = document;
            }
            var allNode = o.getElementsByTagName("*");
            var aNode = [];
            for (var i = 0; i < allNode.length; i++) {
                var re = new RegExp((\\s|^) + cName + (\\s|$));
//                if (allNode[i].className == cName) {
                if (re.test(allNode[i].className)) {
                    aNode.push(allNode[i]);
                }
            }
            return aNode;
        }
        window.onload = function () {
            var oBox = document.getElementById("box2");
            var allNode = getByClass(desc, oBox);
            for (var i = 0; i < allNode.length; i++) {
                allNode[i].style.border = 10px solid red;
            }
        }
    </script>
</head>
<body>
<div id="box">
    <div class="desc desc2"></div>
    <div></div>
    <div class="desc desc2"></div>
</div>
<div id="box2">
    <div class="desc desc2"></div>
    <div class="desc desc2"></div>
    <div class="desc3 desc desc-2"></div>
</div>
</body>
</html>

 

敏感词过滤,js封装class选择器:

标签:head   class选择器   nod   inpu   round   button   regexp   bsp   cti   

原文地址:http://www.cnblogs.com/wangyihong/p/6441067.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!