2017-07-23 16:21 阅读 169

如何为addEventListener的函数添加更多参数? - Javascript

I have a problem when I work with uploadDisplay_SP_reply_'.$statusid.' As you can see these divs has unique id ($statusid) which is auto increment so if I want to display an image inside a specific div in Javascript addEventListener's function the program won't know in which div should it display the image. And here is my problem. In other functions I could add this unique id e.g (replytext_'.$statusid.') in PHP and function showBtnDiv_reply(e) in Javascript where e is $statusid, but in the function completeHandler_reply I can't. The function completeHandler_reply only gets one parameter from the addEventListener which is the event. So how can I add the $statusid to function completeHandler_reply or is there any way I can solve it?


if($isFriend == true || $log_username == $u){
        $statuslist .= '<textarea id="replytext_'.$statusid.'" class="replytext" onfocus="showBtnDiv_reply('.$statusid.')" placeholder="Write a comment..."></textarea>';
        $statuslist .= '<div id="uploadDisplay_SP_reply_'.$statusid.'"></div>';
        $statuslist .= '<div id="btns_SP_reply_'.$statusid.'" class="hiddenStuff">';
            $statuslist .= '<button id="replyBtn_'.$statusid.'" class="btn_rply" onclick="replyToStatus('.$statusid.',\''.$u.'\',\'replytext_'.$statusid.'\',this)">Reply</button>';
            $statuslist .= '<img src="images/camera.png" id="triggerBtn_SP_reply" class="triggerBtn" onclick="triggerUpload_reply(event, \'fu_SP_reply\')" width="22" height="22" title="Upload A Photo" />';
        $statuslist .= '</div>';
        $statuslist .= '<div id="standardUpload_reply" class="hiddenStuff">';
            $statuslist .= '<form id="image_SP_reply" enctype="multipart/form-data" method="post">';
            $statuslist .= '<input type="file" name="FileUpload" id="fu_SP_reply" onchange="doUpload_reply(\'fu_SP_reply\','.$statusid.')"/>';
            $statuslist .= '</form>';
        $statuslist .= '</div>';    


I can add the $statusid here as a parameter e

function showBtnDiv_reply(e){
    _("replytext_"+e).style.height = "130px";
    _("btns_SP_reply_"+e).style.display = "block";

function doUpload_reply(id,sid){
    var file = _(id).files[0];
    if( == ""){
        return false;       

    if(file.type != "image/jpeg" && file.type != "image/gif" && file.type != "image/png" && file.type != "image/jpg"){
        alert("That file type is not supported.");
        return false;
    _("triggerBtn_SP_reply").style.display = "none";
    _("uploadDisplay_SP_reply_"+sid).innerHTML = '<img src="images/rolling.gif" width="30" height="30">';
    var formdata = new FormData();
    formdata.append("stPic_reply", file);
    var ajax = new XMLHttpRequest();
    ajax.addEventListener("load", completeHandler_reply, false);
    ajax.addEventListener("error", errorHandler_reply, false);
    ajax.addEventListener("abort", abortHandler_reply, false);"POST", "php_parsers/photo_system.php");

But here I can't add the $statusid as a 2nd parameter next to event

function completeHandler_reply(event){
    var data =;
    var datArray = data.split("|");
    if(datArray[0] == "upload_complete"){
        hasImage = datArray[1];
        _("uploadDisplay_SP_reply").innerHTML = '<img src="tempUploads/'+datArray[1]+'" class="statusImage" />';
    } else {
        _("uploadDisplay_SP_reply").innerHTML = datArray[0];
        _("triggerBtn_SP_reply").style.display = "block";

function errorHandler_reply(event){
    _("uploadDisplay_SP_reply_").innerHTML = "Upload Failed";
    _("triggerBtn_SP_reply").style.display = "block";

function abortHandler_reply(event){
    _("uploadDisplay_SP_reply").innerHTML = "Upload Aborted";
    _("triggerBtn_SP_reply").style.display = "block";

I've tried it but didn't work for me.

function completeHandler_reply(event,sid){
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    douba9020 douba9020 2017-07-23 16:58

    You could use an anonymouse function within the eventListener and then call the actual function from within that function.

    ajax.addEventListener("load", function(event){ this, arg1,arg2,arg3 );
    }, false );
    ajax.addEventListener("load", function(event){
        completeHandler_reply.apply( this, [ arg1, arg2, arg3 ] );
    }, false );

    Passing in whatever arguments are appropriate ~ I used arg1 etc as an example

    点赞 评论 复制链接分享
  • dongliuliu0385 dongliuliu0385 2017-07-23 17:35

    how about using anonymous function instead? try this:

    ajax.addEventListener("load", function (event){
    }, false);
    点赞 评论 复制链接分享