2017-05-08 27 views
0
function createResendIVButton(userId, name){ 
    return '<span class=btn onclick=usersTable.resendInvestorVerification("'+userId+'","'+name+'")>resend&nbsp;I.V.</span>'; 
} 

當創建一個元素爲HTML,有可能以某種方式發送函數句柄而不是字符串(函數名稱)?字符串不方便,因爲可見性問題。我需要從HTML創建,因爲我使用的庫不會讓我輕鬆訪問它的DOM,但允許我從HTML代碼初始化行。從HTML創建元素,但通過一個函數作爲句柄

+0

的可能的複製[onClick事件添加到使用document.createElement( 「TH」)](http://stackoverflow.com/questions/11017509/add-onclick-event-to- document-createelementth) – rishipuri

+0

@rishipuri你的引用建議使用'appendChild()',但我只能將HTML代碼發送給某個函數。 – exebook

+0

您是否嘗試過使用'eval()'方法? – quirimmo

回答

0

創建上則動作DOM元素和綁定:

 function createResendIVButton(userId, name){ 

      var button = document.createElement('span'); 
      button.className = 'btn'; 

      button.addEventListener('click', function(){ 

       console.log(userId); 
       console.log(name); 

      }); 

      button.innerHTML = 'click on me'; 

      return button; 

     } 
0

喜歡這個?

function createResendIVButton(userId, name) { 
 
    var span = document.createElement('span'); 
 
    span.innerHTML = 'resend I.V'; 
 
    span.onclick = function() { 
 
    usersTable.resendInvestorVerfication(userId, name); 
 
    } 
 
    
 
    var div = document.getElementById('somecontent'); 
 
    div.appendChild(span); 
 
} 
 

 

 
createResendIVButton(1, 'Joe');
#somecontent { 
 
    width: 100px; 
 
    height: 100px; 
 
    background: #ccc; 
 
}
<div id='somecontent'> 
 
</div>

相關問題