使用字符串方法charAt()在javascript

时间:2018-04-23 04:53:47

标签: javascript

我正在尝试解决一个需要我在javascript中使用charAt的问题。 简单问题,让我构建一个表单,使用户能够输入几行文本,然后使用String和charAt搜索字符,以确定搜索字符的出现次数。结果将显示在文本区域中。如果在搜索中找不到该字符,则使用window.open方法在新窗口中显示,并进行如下按钮:“在文本字符串中找不到搜索字符!”

我想出了以下代码,但实际上并没有用,我对出了什么问题感到有些困惑!

<!-- begin snippet: js hide: false console: true babel: false -->
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<title>search using string method</title>

</head>

<body onload="document.getElementById('key').focus();">

<section class="body">
        <h3>Object Exercise Solution 3 - charAt() - Mary had a little lamb</h3>

      <form name="myForm">
         
         <p>Enter a character to search for
            <textarea name="key" rows="3" cols="60" autofocus>
</textarea><br><br>   
<input type="button" value="Go" onclick="search();">
<input type="reset" value="Reset" onclick="document.forms['myForm'].elements['key'].focus();">
</p>
         <br><br>
         
<h3>Occurrences of each digit entered above:</h3>
<textarea name="output" rows="14" cols="23" readonly></textarea>
      </form>
</section>
</body>
</html>

<script>

function search()
         {
            var count=0;

            var key = document.getElementById("key").value.toLowerCase().charAt(0);
             var key = key.toLowerCase();

            var stringToSearch=["a","b","c","d","e","f","g","h","i","j","k","l","m","n",
            "o","p","q","r","s","t","u","v","w","x","y","z"];
            var stringToSearch=stringToSearch.toLowerCase(); 


             for (i=0; i < stringToSearch.length; i++)
                     {

                    if (stringToSearch.charAt(i) == key)
                             count++;     
                     }         

            if ( count == 0 )
               document.getElementById("output").value=key + " not found";
            else
               document.getElementById("output").value=count + 
                  " occurrence(s) of " + key + " found";
         }
</script>

这是我第一次写一个问题“堆栈溢出”我希望我清楚并使用正确的格式! 如果你能提供帮助,请告诉我!

1 个答案:

答案 0 :(得分:-1)

首先:我添加了id ="key"

<textarea id ="key" name="key" rows="3" cols="60" autofocus>
</textarea><br><br>

当您使用document.getElementById('key').focus()时,代码将尝试查找id未使用html定义的元素。

此外,document.getElementById("output").value将找不到任何元素,因为id不存在的元素。所以,改变了以下内容:

<textarea id = "output" name="output" rows="14" cols="23" readonly></textarea>

第二:搜索功能未正确更新,请检查更新后的搜索功能。此外,我将一些代码作为注释,因为它不能用作代码段,取消注释要使用的代码段。

  

下面给出了工作代码段:

&#13;
&#13;
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<title>search using string method</title>

</head>
<script>function search()
         {
            var count={};

            var key = document.getElementById("key").value.toLowerCase();
             var key = key.toLowerCase();

            var stringToSearch=["a","b","c","d","e","f","g","h","i","j","k","l","m","n",
            "o","p","q","r","s","t","u","v","w","x","y","z"];
            var stringToSearch=stringToSearch.join('').toLowerCase(); 

           for(var j = 0; j < key.length;j++) {
            var checkChr = key[j];
             for (i=0; i < stringToSearch.length; i++)
                     {
                    if(!count[stringToSearch.charAt(i)]) {
                      count[stringToSearch.charAt(i)] = 0;
                    }
                    if (stringToSearch.charAt(i) == checkChr)
                             count[stringToSearch.charAt(i)]++;     
                     }
             }

            for (i = 0; i < stringToSearch.length; i++)
            if ( count[stringToSearch.charAt(i)] == 0 )
               document.getElementById("output").value= document.getElementById("output").value + stringToSearch.charAt(i) + " not found\n";
            else
               document.getElementById("output").value=document.getElementById("output").value + count[stringToSearch.charAt(i)] + 
                  " occurrence(s) of " + stringToSearch.charAt(i) + " found\n";
                  alert(document.getElementById("output").value);

//Use the commented line in your real code.
                  //var myWindow = window.open("", "MsgWindow", "width=200,height=100");
    //myWindow.document.write(document.getElementById("output").value);
         }</script>
<body onload="document.getElementById('key').focus();">

<section class="body">
        <h3>Object Exercise Solution 3 - charAt() - Mary had a little lamb</h3>

      <form name="myForm">
         
         <p>Enter a character to search for
            <textarea id ="key" name="key" rows="3" cols="60" autofocus>
</textarea><br><br>   
<input type="button" value="Go" onclick="search();">
<input type="reset" value="Reset" onclick="document.forms['myForm'].elements['key'].focus();">
</p>
<br><br>
         
<h3>Occurrences of each digit entered above:</h3>
<textarea id = "output" name="output" rows="14" cols="23" readonly></textarea>
      </form>
</section>
</body>
</html>
&#13;
&#13;
&#13;