页面内包含一些自定义的文本内容,并提供一个文本框由用户输入全文检索内容。创建一个按钮,并定义按钮的点击事件。
 
当用户点击按钮时,查找页面内包含用户输入内容的文本,并变为红色显示。

解决方案 »

  1.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <style>
    *{margin:0; padding:0;}
    div b{color:red;}
    </style>
    </head><body>   
        <div id="div">哈哈发来得及萨芬健康的撒对方空间撒开飞机升空的风景</div>
        <input type="text" id="text" />
        <input type="button" value="click me" id="btn" />
    <script>

    document.getElementById('btn').onclick = function(){
    var value = document.getElementById('text').value,
    div = document.getElementById('div'),
    str = div.innerHTML,
    str1 = "<b>"+value+"</b>";
    var re = new RegExp(value,"g");

    str = str.replace(re,str1);
    document.getElementById('div').innerHTML = str;


    };

        </script>
    </body>
    </html>
      

  2.   

    提示信息
     
    该回复已经被其他用户举报过了,管理员将尽快处理,如您需要继续举报请到『社区支持』投诉专区进行投诉,或发邮件到[email protected].
      

  3.   

    查看JavaScript使用手册,有很详细的解释