简体   繁体   English

执行javascript程序时出错

[英]Error while executing javascript program

I got an error while executing the following program.I tried to execute the program in head first javascript text book.执行以下程序时出错。我尝试在 head first javascript 教科书中执行该程序。 Please help.请帮忙。 Error:Uncaught TypeError: blog[i].containsText is not a function.错误:未捕获的类型错误:blog[i].containsText 不是函数。

Program:-程序:-

<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>

You have used this within the global scope, when you do this.containsText = fn.当您执行 this.containsText = fn 时,您已经在全局范围内使用了this This means that containsText function is attached to the window object.这意味着 containsText 函数附加到 window 对象。 You should do it within the Blog constructor if you want this method to be available to all instances of Blog constructor.如果您希望此方法可用于 Blog 构造函数的所有实例,则应在 Blog 构造函数中执行此操作。

Try this.尝试这个。

function Blog(body,date)
        {
             this.body=body;
             this.date=date;
              this.containsText = function(str){
              if(this.body.toLowerCase().indexOf(text.toLowerCase())>=0){
                       return true;
                    }
                   return false;
              }
        }

Instead of代替

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

use,用,

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

Because u have to attach containsText method to the prototype of the Blog constructor.因为你必须将 containsText 方法附​​加到博客构造函数的原型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM