我在本规范中遗漏了什么?

时间:2016-11-27 03:27:04

标签: javascript

我是新手,无法弄清楚我哪里出错了。当我点击按钮时没有发生任何事情。我可能添加了太多额外的东西,我无法说清楚。非常感谢任何帮助。

    <META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript" />
    <html ng-app>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="victory.js"></script>
<script language="javascript" type="text/javascript">
var Walmart = (function () {
function Walmart(http, zone) {
    this.stores = [];
    this.storeid = document.getElementById("storenum").value
    this.walmart = {
            query: [
                'http://search.mobile.walmart.com/search?query=',
                '&store=' + this.storeid
            ],
             };
    this.zone = zone;
    this.http = http;  
    console.log(this.walmart);

    };

 var query = document.getElementById("search").value;

Walmart.prototype.getWalmartStoreQuery = function (query) {
    return this.http.get(this.walmart.store.query[0] + query + this.walmart.store.query[1] ).map(function (res) { return res.json(); });

</script>
<tr>
<td><input type="text" name="storenum" id="storenum"></td>
<td><input type="text" name="search" id="search"></td>
</tr>
<tr>
<td><button onclick="getWalmartStoreQuery">Test</button></td>
</tr>

3 个答案:

答案 0 :(得分:1)

如果要通过HTML属性将事件处理程序连接到HTML元素,则代码应为:

onclick="getWalmartStoreQuery()"不是,onclick="getWalmartStoreQuery"

但是,这不是推荐的方法。这种形式的事件布线称为&#34;内联事件布线&#34;和

  1. 创建意大利面条代码,不会促进关注点的良好分离,并使调试更加困难。

  2. 围绕代码创建一个隐藏的全局匿名代理函数,用于操纵this与Global的绑定。

  3. 不遵循W3C DOM Level 2 Event Model并且不像该模型规定的那样强大。

  4. 要以更现代的方式连接事件处理回调,请在加载DOM后立即在JavaScript中执行此操作:

    var btn = document.getElementById("theButtonsId");
    
    // Do not put parenthesis after the callback name as
    // you are only trying to reference it here, not call it
    btn.addEventListener("click", callBackFunctionName);
    

答案 1 :(得分:-1)

测试一下:

&#13;
&#13;
var walmart = function(http, zone){
    		if(!(this instanceof walmart)){
    			return new walmart(http, zone);
    		}
    
    	   this.stores = [];
        this.storeid = document.getElementById("storenum").value
        this.walmart = {
            query: [
                    'http://search.mobile.walmart.com/search?query=',
                    '&store=' + this.storeid
              ],
          };
        this.zone = zone;
        this.http = http;
    };
    
    walmart.prototype.getWalmartStoreQuery = function () {
    	var query = document.getElementById("search").value;
        return this.http.get(this.walmart.store.query[0] + query + this.walmart.store.query[1] ).map(function (res) {
        	return res.json();
        });
    };
    
&#13;
    <tr>
    <td><input type="text" name="storenum" id="storenum"></td>
    <td><input type="text" name="search" id="search"></td>
    </tr>
    <tr>
    <td><button onclick="walmart.prototype.getWalmartStoreQuery()">Test</button></td>
    </tr>
&#13;
&#13;
&#13;

答案 2 :(得分:-3)

您无法在按钮

的onclick事件中实例化类中的函数