Javascript学习:综合案例6--字符串筛选

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        h2{text-align: center;}
        div{margin:0 auto;width:500px;background:burlywood;padding:30px;text-align: right;}
        input{width:360px;height:20px;line-height:20px;padding:10px;}
        .btn{height:40px;width:100px;}
    </style>
</head>
<body>

<h2>字符串筛选</h2>
<div>
    <p>示例字符串:<input type="text" id="str" name="str" value=""></p>
    <p>筛选部分:<input type="text" id="st" name="st" value=""></p>
    <p>筛选结果:<input type="text" id="res" name="res" value=""></p>
    <input type="button" value="测试按钮" onclick="replace()" class="btn">
</div>

<script>
    
    function replace() {
    str = document.getElementById("str").value;
    st = document.getElementById("st").value;
    res = document.getElementById("res");
    for(var i=0;i<str.length;i++){
    if(str.indexOf(st)>-1){
    str=str.replace(st,"");
    //把特定字符串转换成空字符串
    }else{
    break;
    //如果没有,则跳出循环
    }
    }

    res.value=str;
    }




</script>

</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_32584661/article/details/80761458