[removed] pass an object as the argument to a onclick function inside string

前端 未结 3 1409
温柔的废话
温柔的废话 2020-12-30 00:15

I would like to pass an object as parameter to an Onclick function inside a string. Something like follwing:

function myfunction(obj,parentobj){ 
   var o=do         


        
3条回答
  •  北海茫月
    2020-12-30 00:59

    The above example does not work because the output of obj to text is [Object object], so essentially, you are calling someListener([Object object]).

    While you have the instance of the element in o, bind to it's click using javascript:

    function myfunction(obj,parentobj){ 
        var o=document.createElement("div");
        o.innerHTML='';
    
        o.onClick = function () {
            someListener(obj)
        }
    
        parentobj.appendChild(o.firstChild);
    }
    

    I have created a working fiddle for you here: JSFiddle

提交回复
热议问题