【问题标题】:javascript oo questionjavascript oo问题
【发布时间】:2009-12-04 04:49:50
【问题描述】:

我有一个这样的html页面

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script src="JScript2.js" type="text/javascript"></script>
    <script src="JScript.js" type="text/javascript"></script>
    <title></title>
</head>
<body >
    <div ><input type=text id="uxTextBox" /></div>
</body>
</html>

还有 2 个这样的 javascript 文件

/// <reference path="JScript.js"/>
/// <reference path="jquery-1.3.2-vsdoc.js"/>
/// <reference path="jquery-1.3.2.js" />

$(document).ready(function() {
    referral.PopulateTextBox();

}

=====第一个文件结束==========文件

/// <reference path="jquery-1.3.2-vsdoc.js"/>
/// <reference path="jquery-1.3.2.js" />


var referral = new Referral(); 
$(document).ready(function() {
function Referral() {
    this.PopulateTextBox = function() {
    $("#uxTextBox").text("some text");


    }
}

}

问题是 2 个 jquery 文件似乎都没有执行。我正在尝试从对另一个 js 文件的调用中填充一个对象,然后将值返回给 html 文件

有什么想法吗?

【问题讨论】:

    标签: javascript jquery oop


    【解决方案1】:

    这是关于作用域的,JavaScript 具有函数作用域,因此您在 $(document).ready 回调函数中所做的所有变量和函数声明都只能在该作用域内访问。

    例如:

    $(document).ready(function() {
    
      function Referral() {
        // ...
      }
    
      // Referral is accessible here
    });
    // But not here
    

    您可以在全局范围内声明您的 Referral constructor function,因为您打算从多个文件中使用它。

    如果你不喜欢全局变量,你可以实现 namespacing 技术:

    有关范围的更多信息:

    【讨论】:

      【解决方案2】:

      您需要确保在您的 html 页面中包含 jQuery 源代码。 “参考”cmets 看起来像是由您的编辑器插入的东西,因此它可以确定您正在使用哪些文件,但这些 cmets 确实告诉浏览器从哪里获取 jQuery 源代码。

      将此作为第一个 &lt;script&gt; 标记在您的文档中:

      <script src="/path/to/jquery.js"></script>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-04
        • 2011-01-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多