2016-12-30 60 views
0

有什麼方法可以選擇當前標籤作爲DOM元素而不使用id屬性?如何在riotjs中選擇當前標籤作爲DOM元素

我現在在做的是;

function addAttr(n,v){ 
    var ta = document.getElementById(opts.id); 
    ta.setAttribute(n,v); 
} 

我正在尋找一些像這樣的直接方式;

function addAttr(n,v){ 
    var ta = document.querySelector(this); 
    //var ta = document.querySelector(this.root); 
    ta.setAttribute(n,v); 
} 

傳遞this.root作品與jQuery,但不與document.querySelector()

Complete example

回答

0

我得到了解決。我在「掛載」事件中移動了我的代碼。所以this.root可用。現在我不需要查詢選擇器。 this.root本身起作用。

現在我可以直接使用;

this.root.setAttribute(n,v); 

我已更新plunker鏈接以供參考。

this.on("mount",function(){ 
    if(opts.fa){ 
    this.root.className += " faa"; 
    if(opts.selectable) 
     this.root.innerHTML = fa_icons[opts.fa]; 
    else{ 
     this.root.setAttribute("faicon", fa_icons[opts.fa]); 
    } 
    }else if(opts.text){ 
    this.root.innerHTML = opts.text; 
    } 

    if(opts.transform){ 
    this.root.style.transform = opts.transform; 
    } 
}); 
相關問題