2016-12-15 3 views
0

私はdojoを使用してプログラムでdom要素を作成していますが、idでDOM内にDOM要素を表示できますが、dom.byId( "myId ")はnullを返します。dojoを使用するdom.byIdはプログラムで要素を追加していません

私は実際に働いているので(私の問題を再現しませんが、私が何をしようとしているのかが分かります):ボタンをクリックすると(スタイリングの不足は無視されます)実行出力パネルで、dom.byIdによって取り出された要素の内容を警告します。しかし、私のdojoウィジェット内の同様のコードは機能しません。ここでは、コードです:

 var content = lang.replace(selectFilterTemplate, { 
      "layer-id": layer.id, 
      "layer-index": idx, 
      "filter-name": filter.name 
     }); // this gets template HTML code similar to what's in the HTML panel of the jsfiddle, only it has placeholder tags {} instead of literals, and the tags are replaced with the attributes of the layer, idx, and filter objects here 

     // Use dojo dom-construct to create a div with the HTML from above 
     var node = domConstruct.create("div", { "innerHTML": content }); 

     // put the new div into a dojo ContentPane 
     var filterPanel = new ContentPane({ 
      "id": layer.id + "-filter-" + idx + "-panel", 
      "content": node, 
      "style": "width: 200px; float: left;" 
     }); 

     // Get the dom element: 
     var mstag = dom.byId(layer.id + "-filter-" + idx + "-ms-tag") 
     // this is the same as the "var ms = dom.byId("IssuePoints-filter-1-ms-tag")" in the jsfiddle, but this one returns null. If I view the contents of the 'node' variable in the browser debugging console at this point, I can see the <select> tag with the id I'm referencing. 

私がデバッグコンソールでDOMにその要素を見ることができれば、なぜ私は私のdom.byId()でnullを取得するのでしょうか?

答えて

0

後でdomに要素が追加されたようです。あなたはデバッガでそれを見るかもしれませんが、あなたがbyId()を呼び出した瞬間にはまだ利用できません。

投稿したコードでは、filterPanel要素を作成しますが、domに配置しないでください。私はこれが後の段階で起こると仮定します。これに対して、jsfiddleは、Button要素を作成した直後にplaceAt()を配置します。

+0

ありがとうpgianna。新しいContentPane()の終わりに.plactAt(document.body)を置くことは、私のためのトリックでした。 – user2193215

関連する問題