执行javascript程序时出错

时间:2015-11-30 07:10:08

标签: javascript html

我在执行以下程序时遇到错误。我试图在头部第一个javascript教科书中执行该程序。请帮忙。错误:未捕获TypeError:blog [i] .containsText不是函数。

程序: -

<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="msapplication-tap-highlight" content="no" />
        <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <title>Hello World</title>
        <script type="text/javascript">
           function Blog(body,date)
            {
                 this.body=body;
                 this.date=date;
            }
            var blog=[new Blog("Got the new cube I ordered...","08/14/2008"),
                        new Blog("Solved the new cube of course...","08/19/2008"),
                         new Blog("Managed to get a headache to iling...","08/14/2008"),
                         new Blog("Found a 7X7X7 cube for sale online..","08/14/2008")];

        function showBlog(numEntries)
        {
                 if(!numEntries)
                        numEntries=blog.length;
                var i=0, blogText="";
                while(i<blog.length&&i<numEntries)
                {
                     if(i%2==0)
                        blogText+="<p style='background-color:#EEEEEE'>";
                    else
                         blogText+="<p>";

                   // alert(blog[i].date+" "+blog[i].body);

                    blogText+="<strong>"+blog[i].date+"</strong><br/>"+blog[i].body+"</p>";
                    i++;
                    //alert(blogText);
                }
                var blogDate=new Date("08/14/2008");
                alert(blogDate.toString());                 
                // document.getElementById('pi').innerText.containsText;
                var searchText=document.getElementById('pi').value;
                for(var i=0;i<blog.length;i++)
                {
                    if(blog[i].containsText(searchText))
                    {

                        alert(blog[i]);
                    }
                }
                 document.getElementById('blog').innerHTML=blogText;
        }

        this.containsText=function(text)
        {
            alert(this.body.toLowerCase().indexOf(text.toLowerCase()));
        };
        </script>

        </head>    


            <body onload="showBlog(5);">
  <h3> YouCube- The Blog for cube puzzlers</h3>
  <img src="img/Cube-with-blender.png" alt="YouCube"/>
  <br/>
  <br/>
  <br/>
  <br/>  
  <div id="blog"></div>
  <p id="pi">Got the new cube I ordered</p>
  <input type="button" id="showall" value="show all blog entries" onclick="showBlog();"/>
        </body>
     </html>

2 个答案:

答案 0 :(得分:1)

在执行此操作时,您已在全局范围内使用public String isRemovableSDCardAvailable() { final String FLAG = "mnt"; final String SECONDARY_STORAGE = System.getenv("SECONDARY_STORAGE"); final String EXTERNAL_STORAGE_DOCOMO = System.getenv("EXTERNAL_STORAGE_DOCOMO"); final String EXTERNAL_SDCARD_STORAGE = System.getenv("EXTERNAL_SDCARD_STORAGE"); final String EXTERNAL_SD_STORAGE = System.getenv("EXTERNAL_SD_STORAGE"); final String EXTERNAL_STORAGE = System.getenv("EXTERNAL_STORAGE"); Map<Integer, String> listEnvironmentVariableStoreSDCardRootDirectory = new HashMap<Integer, String>(); listEnvironmentVariableStoreSDCardRootDirectory.put(0, SECONDARY_STORAGE); listEnvironmentVariableStoreSDCardRootDirectory.put(1, EXTERNAL_STORAGE_DOCOMO); listEnvironmentVariableStoreSDCardRootDirectory.put(2, EXTERNAL_SDCARD_STORAGE); listEnvironmentVariableStoreSDCardRootDirectory.put(3, EXTERNAL_SD_STORAGE); listEnvironmentVariableStoreSDCardRootDirectory.put(4, EXTERNAL_STORAGE); File externalStorageList[] = null; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) { externalStorageList = getContext().getExternalFilesDirs(null); } String directory = null; int size = listEnvironmentVariableStoreSDCardRootDirectory.size(); for (int i = 0; i < size; i++) { if (externalStorageList != null && externalStorageList.length > 1 && externalStorageList[1] != null) directory = externalStorageList[1].getAbsolutePath(); else directory = listEnvironmentVariableStoreSDCardRootDirectory.get(i); directory = canCreateFile(directory); if (directory != null && directory.length() != 0) { if (i == size - 1) { if (directory.contains(FLAG)) { Log.e(getClass().getSimpleName(), "SD Card's directory: " + directory); return directory; } else { return null; } } Log.e(getClass().getSimpleName(), "SD Card's directory: " + directory); return directory; } } return null; } /** * Check if can create file on given directory. Use this enclose with method * {@link BeginScreenFragement#isRemovableSDCardAvailable()} to check sd * card is available on device or not. * * @param directory * @return */ public String canCreateFile(String directory) { final String FILE_DIR = directory + File.separator + "hoang.txt"; File tempFlie = null; try { tempFlie = new File(FILE_DIR); FileOutputStream fos = new FileOutputStream(tempFlie); fos.write(new byte[1024]); fos.flush(); fos.close(); Log.e(getClass().getSimpleName(), "Can write file on this directory: " + FILE_DIR); } catch (Exception e) { Log.e(getClass().getSimpleName(), "Write file error: " + e.getMessage()); return null; } finally { if (tempFlie != null && tempFlie.exists() && tempFlie.isFile()) { // tempFlie.delete(); tempFlie = null; } } return directory; } .containsText = fn。这意味着containsText函数附加到window对象。如果您希望此方法可用于Blog构造函数的所有实例,则应在Blog构造函数中执行此操作。

试试这个。

this

答案 1 :(得分:1)

而不是

this.containsText=function(text){
         alert(this.body.toLowerCase().indexOf(text.toLowerCase()));
 };

使用,

Blog.prototype.containsText=function(text)
        {
            alert(this.body.toLowerCase().indexOf(text.toLowerCase()));
        };

因为你必须将containsText方法附加到Blog构造函数的原型。