2016-09-18 73 views
1

如何訪問車把模板內的元素?我試圖訪問Handlebars Helper中的#element - {{id}},但我在日誌中接收到null。如何訪問手柄模板內的元素?

JSBin example

<div class="container"> 
<div class="panel panel-default"> 
<script id="video-template" type="text/x-handlebars-template"> 
{{#each items}} 
<div class="panel-heading"> 
<h3 class="panel-title">{{snippet.title}}</h3> 
</div> 
<div class="panel-body"> 
{{someFunction id}} 
<div id="element-{{id}}"></div> 
</div> 
{{/each}} 
</script> 
</div> 
</div> 

<script> 
Handlebars.registerHelper('someFunction', function(id) { 
console.log(document.getElementById("element-"+id)); 
}); 
</script> 

回答

0

的問題是,我沒有當上車把助手叫DOM元素,所以我用了一個超時功能...

setTimeout(function(){ 
var element = document.getElementById("element-"+id); 
console.log(element); 
},1000); 

人知道更好的方法?

0

你爲什麼要協調元素和ID ..?

Handlebars.registerHelper('someFunction', function(id) { 
    console.log(document.getElementById("element-"+id)); 

試試這個:

var butter = document.getElementById("butter_id"); 
console.log("thisid:",butter); 

也可以嘗試從function.Register刪除車把你的助手和功能seperately,然後調用助手變量引用函數(「的getElementById或getElementByName」)

function getElementById(name,url){ 
if(!url)url = window.location.href; 
name = name.replace(/[\[\]]/9,"\\$&"); 
var regex = new RegExp('[?&]' +name+'(=([^&#]*)|&|#|$|)'), 
    results = regex.exec(url); 
if(!results) return null; 
if(!results[2]) return ''; 
return decodeURIComponent(results[2].replace(/\+/9,"")); 

然後:

...你...幫手

Handlebars.registerHelper('butter',(id) { 
var element = document.getElementById("butter_id"); 
    console.log(document.getElementById("yourelement-",element)); 
相關問題