2016-09-14 28 views
1

我有一個包含嵌套的標籤...... 例如防暴標籤:riot.js嵌套標記 - 我如何選擇一個內部的HTML元素與jQuery?

<parent> 
<!-- dome html here...--> 
    <child></child> <!-- visually - the child is mounted correctly !--> 

    <script> 
     this.on('mount', function(){ 
      $('select').material_select(); 
     }); 
    </script> 
</parent> 

<child> 
    <select> 
     <option values="1">1</option> 
     <option values="2">2</option> 
     <option values="3">3</option> 
    </select> 
    <script> 
     this.on('mount', function(){ 
      $('select').material_select(); 
     }); 
    </script> 
</child> 

現在,我想用Materialise的庫和jQuery做出的排行榜 物化設計,如http://materializecss.com/forms.html 它通常在'select'標籤位於父標籤時工作正常!

但是,我找不到在哪裏初始化$('select').material_select(); 命令在子中,因此,子標記中的選擇標記是不可見的!

我試圖初始化它在父(和)子標籤的on('mount')區域 - 但它似乎總是$('選擇')選擇器返回一個空數組 - 任何想法?

+0

我現在處理它的方式真的不是最佳的...我初始化$(「選擇」)material_select()。在函數中調用它1000毫秒後,使用setTimeout() - 這個工程,但真的不優雅 - 是否有一個更優雅的方式來知道什麼時候嵌套的標記安裝? –

+0

你試過'$('select',this.root).material_select();'在子標記掛載事件中嗎? – Heikki

+0

令人沮喪的工作在我的jsFiddle:https://jsfiddle.net/ghstahl/pob86fj3/,但不是在我的webpacked應用程序。我必須在我的坐騎上做100ms的定時器來做最後的呼叫。 –

回答

1

您可以嘗試強制孩子更新,然後調用孩子上的on('updated')事件,因爲這似乎是DOM沒有準備好onMount。檢查這個例子。

<parent> 
<!-- dome html here...--> 
    <child></child> <!-- visually - the child is mounted correctly !--> 

    <script> 
     this.on('mount', function(){ 
      this.update(); 
     }); 
    </script> 
</parent> 

<child> 
    <div> 
    <select> 
     <option values="1">1</option> 
     <option values="2">2</option> 
     <option values="3">3</option> 
    </select> 
    </div> 
    <script> 
     this.on('updated', function(){ 
      $('select').material_select(); 
     }); 
    </script> 
</child> 

網絡版https://jsfiddle.net/vitomd/Lm4dy21d/5

+1

當你使用最新的(v2.6.1)暴亂版本 - > https://jsfiddle.net/qbjw4sq8/ – Heikki

+1

時,你的例子沒有任何竅門,這裏是Heikki小提琴更復雜的變體。 https://jsfiddle.net/ghstahl/pob86fj3/ 我使用動態創建的ID並掛鉤更改事件。 –