簡單查找替換

效果圖

示例代碼

html文件:

<div id="all">
    <div class='content'>
        <p>從最基礎的 HTML5移動端&PC端布局、CSS3動畫、3D交互特效、JS原理、JS基礎、
          JS中級的DOM\BOM\EVENT、ajax數據交互,再到JS的面向對象、JS組件開發、JS模
          塊化開發(seaJS)、 正則表達式、node.js、jQuery庫實戰教程、JQ源碼分析、
          PC端布局實戰、電商網站實戰、HTML5+CSS3實戰、移動端實戰、交互動畫教程、
          前端面試題、canvas及游戲、html5視頻音頻、html5地理位置信息、 html5跨文檔
          消息通信……
        </p>
    </div>
    <div class="btn">
        <input type="button" value="展開">
        <input type="button" value="查找">
        <input type="button" value="替換">
    </div>
</div>
<div id="box">
    <ul class="nav">
        <li class="active"><a href="#">查找</a></li>
        <li><a href="#">替換</a></li>
        <li class="closed"><a href="#">X</a></li>
    </ul>
    <div class="find">
        <input type="text" name="" value="">
        <input type="button" name="" value="查找">
    </div>
    <div class="change">
        <input type="text" name="" value="">
        <input type="text" name="" value="">
        <input type="button" name="" value="替換">
    </div>
</div>

css代碼:

* {
    padding: 0;
    margin: 0;
    font-family: "micrisoft YaHei";
}
li {
    list-style: none;
}
a {
    text-decoration: none;
}
body {
    background: #efefe7;
    padding: 30px 300px;
}
#all {
    overflow: hidden;    width: 600px;
}
.content {
    float: left;
    padding: 30px 20px;
    width: 460px;
    height: 190px;
    background: #fff;
    margin: 0 10px;
}
p {
    font-size: 15px;
    line-height: 30px;
    font-family: "Mircrosoft YaHei"
}
.btn {
    width: 80px;
    float: left;
    /*display: none;*/
}
.btn input {
    height: 40px;
    line-height: 40px;
    width: 80px;
    margin-bottom: 2px;
    border: none;
    background: #ccc;
    color: #fff;
    font-size: 16px;
}
.btn input:hover{
    background: #999;
}
#box {
    border: 10px solid yellowgreen;
    width: 460px;
    margin-top: 10px;
    padding: 20px;
    display: none;
}
.nav {
    height: 30px;
    border-bottom: 2px solid coral;
    margin-bottom: 8px;
}
.nav li {
    float: left;
    padding: 0 30px;
    height: 30px;
    line-height: 30px;
}
.nav .closed {
    float: right;
    background: none;
    padding: 0;
}
.nav a {
    color: #000;
}
.nav li:hover, .nav .active {
    background: coral;
}
.nav li:hover a, .nav .active a {
    color: #fff;
}
.nav .closed a {
    color: #000;
}
.nav .closed:hover {
    background: none;
}
.nav .closed:hover a {
    color: #999;
}
#box input {
    height: 30px;
}
.change {
    display: none;
}

js代碼:

var oAll = document.getElementById('all'),
        oP = document.getElementsByTagName('p')[0],
        oBtn = oAll.getElementsByTagName('div')[1],
        aInp = oAll.getElementsByTagName('input'),
        oBox = document.getElementById('box'),
        oUl = oBox.getElementsByTagName('ul')[0],
        aLi = oUl.getElementsByTagName('li'),
        aDiv = oBox.getElementsByTagName('div'),
        onOff = true;
fn2();
aInp[0].onclick = function () {
    aInp[0].value = '展開';
    if (onOff) {
        aInp[1].style.display = 'block';
        aInp[2].style.display = 'block';
        aInp[1].onclick = function () {
            fn1();
            find();
            aInp[0].value = '查找';
        };
        aInp[2].onclick = function () {
            fn1();
            change();
            aInp[0].value = '替換';
        }
    } else {
        fn2();
        aInp[0].value = '展開';
    }
    onOff = !onOff;
};
aLi[0].onclick = function () {
    find();
};
aLi[1].onclick = function () {
    change();
};
aLi[2].onclick = function () {
    oBox.style.display = 'none';
};
function fn1() {
    oBox.style.display = 'block';
    aInp[1].style.display = 'none';
    aInp[2].style.display = 'none';
    onOff = !onOff;
}
function fn2() {
    aInp[1].style.display = 'none';
    aInp[2].style.display = 'none';
}
function find() {
    aDiv[0].style.display = 'block';
    aDiv[1].style.display = 'none';
    aLi[0].className = 'active';
    aLi[1].className = '';
}
function change() {
    aDiv[0].style.display = 'none';
    aDiv[1].style.display = 'block';
    aLi[1].className = 'active';
    aLi[0].className = '';
}
var aFind = aDiv[0].getElementsByTagName('input');
var aChange = aDiv[1].getElementsByTagName('input');
aFind[1].onclick = function () {
    if (aFind[0].value == "") {
        alert("請輸入內容!");
    } else if (oP.innerHTML.indexOf(aFind[0].value) == -1) {
        alert("沒有找到相同的內容");
    } else {
        var str = oP.innerHTML.split(aFind[0].value);
        oP.innerHTML = str.join("<span style='color:red;background: #ccc;'>" + aFind[0].value + "</span>");
    }
};
aChange[2].onclick = function () {
    if (aChange[0].value == "") {
        alert("請輸入內容!");
    } else if (oP.innerHTML.indexOf(aChange[0].value) == -1) {
        alert("沒有相同的內容");
    } else { 
       var str = oP.innerHTML.split(aChange[0].value);
        oP.innerHTML = str.join("<span style='color:red;background: #ccc;'>" + aChange[1].value + "</span>");
    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,813評論 25 708
  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,180評論 4 61
  • #1 跑完步一身輕松的感覺是很不錯的。 仿佛一些不愉快的事就隨汗水而消散在空氣中了,然后內心也會更加清明。盡管在那...
    方曉薏閱讀 562評論 2 5
  • 我一直學習都不好,總是為自己的不努力學習找這樣那樣的理由。 不努力學習也就算了,而且還總是會對那些比我努力比我學習...
    烏卓閱讀 770評論 2 6
  • 陽光下的溫暖 今年冬天感觸最深的事兒就是陽光燦爛的日子總是那么少。剛入冬時的一場大雪把我們從秋的多彩瞬間就帶入冬的...
    夢熙兒閱讀 248評論 0 2