2017-03-08 44 views
0

我在javascript文件中創建html,我試圖點擊標籤,我已經在標籤中添加了editNote(),並且我已將其稱爲邊,但是我的問題是,editNote( )函數無法執行。那麼你能告訴我如何執行這個功能嗎?Onclick不工作<a>標籤

html += '<a onclick="editNote(' + note.id + ', ' + userId + ')" style="cursor:pointer"><i style="color:blue" class="fa fa-pencil-square-o fa-2x"></i></a>'; 

功能

function editNote(noteId, userId) { 
    alert(noteId); 
} 
+0

請完成後在你寫這條線。 –

+0

你的控制檯中有javascript錯誤嗎? – userfloflo

+0

@ C0dekid我不認爲這是問題,因爲這對我沒有href屬性 – smarber

回答

3

它爲我稍作修改。請考慮下面的代碼

var note = {id: 5}, 
 
    userId = 56; 
 

 
html = '<a onclick="editNote(' + note.id + ', ' + userId + ')" style="cursor:pointer;" href="#">button</a>'; 
 

 
document.write(html); 
 

 
function editNote(noteId, userId) { 
 
    alert(noteId); 
 
}
<div></div>

0

試試這個腳本... 我希望這種幫助的

<div id="click">your code here</div> 

<script type="text/javascript"> 

    var noteId = 1; 
    var userId = 2; 

    var html = '<a onclick="editNote('+noteId+','+userId+')" style="cursor:pointer"><i style="color:blue" class="fa fa-pencil-square-o fa-2x">CLICK ME</i></a>'; 

    document.getElementById("click").innerHTML = html; 

    function editNote(noteId, userId) { 
     alert(noteId); 
     alert(userId); 
    } 
</script>