2017-02-17 41 views
0

我嘗試包括2個元素的html文件的樣子:選擇鐵的Ajax元素

<html> 
    <body> 
    <iron-ajax id="requestContent"></iron-ajax> 
    <my-custom-element></my-custom-element> 
    </body> 
</html> 

my-custom-element我有一個鏈接標籤與on-click財產,我想id選擇iron-ajax元素,當我點擊在my-custom-element上的鏈接。我怎樣才能做到這一點?

<dom-module id="my-custom-element"> 
    <template> 
    <a href$="/target-page" on-click="_aFunction">click me</a> 
    </template> 
    <script> 
    Polymenr({ 
     is: 'my-custom-element', 
     _aFunction: function(){ 
      console.log(this.parentNode); // output element body 
      console.log(this.parentNode.$); // undefined 
      // console.log(this.parentNode.$.requestContent); 
     } 
    }); 
    </script> 
</dom-module> 
+0

這可能不是一個好主意。這是因爲'my-custom-element'必須有一個擁有'iron-ajax'作爲孩子的父母。真的,你應該將一個事件發送到父元素,然後可以決定進程執行。查看[dom-bind](https://www.polymer-project.org/1.0/docs/api/dom-bind)模板和[解僱自定義事件](https://www.polymer-project .org/1.0/docs/devguide/events#custom-events) –

+0

@BenThomas fyi,如果我把'iron-ajax'和'my-custom-element'設置爲另一個自定義元素,但是我想要在body標籤裏面的元素 – itx

回答

0

孩子觸發要選擇與ID的自定義事件, 父觸發選擇ID的功能。

家長

<iron-ajax id="requestContent" on-select-ajax="handleSelection"></iron-ajax> 

      handleSelection: function(e) { 
      // do what you have to do with e.name 
      // you can pass the data anywhere 
      } 

兒童

<dom-module id="my-custom-element"> 
    <template> 
    <a href$="/target-page" on-tap="_aFunction">click me</a> 
    </template> 
    <script> 
    Polymenr({ 
     is: 'my-custom-element', 
     _aFunction: function(e, detail) { 
     this.fire('select-ajax', {name: requestContent}); 
     } 
    }); 
    </script> 
</dom-module> 

PS:我改變了對點擊就抽頭,讓您的鏈接的點擊和觸摸。