2016-07-21 4 views
-2

私は関数を呼び出すか、オブジェクトマップエリアに関数を作成したいと思っています。オブジェクト内の関数を呼び出す/使用する方法は?

どのように正常に機能しますか?これは私が得たものです。 ...しかし

var _images = { 
    test: { 
     path: 'test.jpg', 
     areas: [{ 
      coords: '211,38,238,60', 
      text: 'let me test it', 
      //onclick: .... ? 
      obj = function() { 
       this.hello = function hello() {} 
       alert("Hello"); 
      } 
     }] 
    }, 
} 
var asdf = new obj(); 
asdf.hello(); 
+1

動作しないとはどういう意味ですか?何かエラーがありますか? – thefourtheye

+0

何も起こらない^^私はonclickで関数を宣言する必要がありますか? – Emloy

+1

これは何をする*と考えられていますか?...! – deceze

答えて

-2

对象属性应该用:

var _images = { 
    test: { 
     path: 'test.jpg', 
     areas: { 
      coords: '211,38,238,60', 
      text: 'let me test it', 
      //onclick: .... ? 
      obj:function() { 
       this.hello = function hello() {}; 
       console.log('hello') 
      } 

     } 
    } 
}; 

var asdf = new _images.test.areas.obj(); 
asdf.hello(); 
+1

あなたは英語で書くべきです!これはローカルサイトではありません –

1

JavaScriptオブジェクト機能を動作しないことである ":" ではありません "="!以下のように、新しいインスタンスオブジェクトをパスとして呼び出すことができます。

var _images = { 
    test: { 
    path: 'test.jpg', 
    areas: [{ 
     coords: '211,38,238,60', 
     text: 'let me test it', 
     //onclick: .... ? 
     obj : function() { 
     this.hello = function hello() {} 
     alert("Hello"); 
     } 

    }] 
    }, 
} 
var asdf = new _images.test.areas[0].obj(); 
asdf.hello(); 
+0

しかし、どうすればcoordsをクリックすると呼び出せますか? '211,38,238,60'、? – Emloy

+0

あなたがクリックしたい場合は、htmlに追加する必要があります。どのように(どこで)あなたはコインをクリックしますか? –

+0

私のhtmlファイルはjsファイルのみを呼び出します。 DOM-Elementsを使って画像と地図の領域を作成しました。 – Emloy

関連する問題