Jquery突出显示功能不起作用

时间:2016-04-06 13:56:24

标签: jquery

我正在尝试查找文本框文本中的所有数值并突出显示它们但没有发生任何事情。代码如下。你能否帮助确定我做错了什么?

 model Notes.Models.NotesModel
 <html>
   <head>
      <title>Notes</title>
       <style>
            .highlight {background-color :yellow;}
       </style>
       <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
       <script type="text/javascript" language="javascript">

          function PIIHighlight() {
                                 var regex = new RegExp("\D+", "gi");
                                 alert(document.getElementById("Notes").value.toString());
                                 return $("Notes").each(function ()    {  document.getElementById("Notes").value.replace(regex,function (matched) { return "<span class=\"" + highlight + "\">" + matched + "</span>"; });
                                   });
                               };
       </script>
    </head>

    <body>
       <p> <input id="Notes" name="notes" type="text" spellcheck="true" class="txtbox" style="font-size:12pt;height:220px;width:600px;" /></p>
       <p> <button onclick="PIIHighlight();">Check</button></p>
    </body>
</html>

谢谢!

1 个答案:

答案 0 :(得分:0)

这让我觉得你看起来更有吸引力,但依赖于满足的属性......

model Notes.Models.NotesModel 
<html>
 <head>
  <title>Notes</title>
   <style>
        .highlight {background-color :yellow;}
   </style>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
   <script type="text/javascript" language="javascript">

       $(document).ready(function(){document.getElementById("Notes").focus();});
      function PIIHighlight() {
                             var values = document.getElementById("Notes").innerHTML.split(" ");
                             values.forEach(function(elem, idx){
          //$("values").each(function(){ 
                                 if(!isNaN(elem))
                                     {
                                         values[idx] = "<span class='highlight'>" + elem + "</span>";
                                     }
                               });
                             values = values.join(" ");
                             document.getElementById("Notes").innerHTML = values;
                           }
   </script>
 </head>

 <body>
    <p> <div id="Notes" contenteditable="true" name="notes" type="text" spellcheck="true" class="txtbox" style="font-size:12pt;height:220px;width:600px;padding:5px; border:1px solid blue;"></div></p>
   <p> <button onclick="PIIHighlight();">Check</button></p>
 </body>
</html>

希望它有所帮助。

相关问题