Onclick无效<a> tag

时间:2017-03-08 13:01:04

标签: javascript html

I am creating html in javascript file, and i am trying to click on tag, i have added editNote() in tag, and i have called it out side, but my problem is , editNote() function can't execute. So can you tell me how to execute this function ?

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

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

2 个答案:

答案 0 :(得分:3)

稍微修改了它对我有用。请考虑以下代码

&#13;
&#13;
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);
}
&#13;
<div></div>
&#13;
&#13;
&#13;

答案 1 :(得分: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>