javascript:将对象作为参数传递给字符串内的 onclick 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12109811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:07:31  来源:igfitidea点击:

javascript: pass an object as the argument to a onclick function inside string

javascriptargument-passing

提问by Elizabeth

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

我想将一个对象作为参数传递给字符串内的 Onclick 函数。类似于以下内容:

function myfunction(obj,parentobj){ 
   var o=document.createElement("div");
   o.innerHTML='<input type="button" onclick="somelistener(' + obj + ')" />';
   parentobj.appendChild(o.firstChild);
}

Obviously, this doesn't work. Anyone has any idea? THX!

显然,这行不通。任何人有任何想法?谢谢!

A more complete version, as suggested by @Austin

更完整的版本,如@Austin 所建议的

<!DOCTYPE html>
<html>
<body>
<style type="text/css">

</style>
<p id="test">test</p>
<p id="objectid"></p>

<script>
function test(s){
    document.getElementById("test").innerHTML+=s;
}

function somelistener(obj){
    test(obj.id);
}

function myfunction(obj,parentobj){ 
    var o=document.createElement("div");
    o.innerHTML='<input type="button" onclick="somelistener(' + obj + ')" />';

    o.onclick = function () {
        someListener(obj)
    }
parentobj.appendChild(o.firstChild);
}

myfunction(document.getElementById("objectid"),document.getElementById("test"));

</script>

</body>
</html>

回答by Austin

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

上面的示例不起作用,因为objto text的输出是[Object object],所以本质上,您正在调用someListener([Object object]).

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

当您在 中拥有元素的实例时o,请使用 javascript 绑定到它的点击:

function myfunction(obj,parentobj){ 
    var o=document.createElement("div");
    o.innerHTML='<input type="button" />';

    o.onClick = function () {
        someListener(obj)
    }

    parentobj.appendChild(o.firstChild);
}

I have created a working fiddle for you here: JSFiddle

我在这里为您创建了一个工作小提琴:JSFiddle

回答by Nikikiy SCC

function myfunction(obj,parentobj){ 
        var o=document.createElement("div");
         o.innerHTML="<input type='button' onclick='somelistener("+JSON.stringify(obj)+")'/>"; 
                                  parentobj.appendChild(o.firstChild);
            } 
    // my similar problem, function a was called in a jsonArray loop in the dataTable initiation
    function a(data, type, obj) {
                         var str = "";
                         str += "<span class='button-group'>";
                         str +="<a onclick='chooseData1("+JSON.stringify(obj)+")'>[选择]</a>";
                         str += "</span>";
                         return str;
                                            }
function chooseData1(data){
                        console.log(data);
                  }

回答by Jaskaran Singh

Just do pass it like this :

就像这样通过它:

o.innerHTML='<input type="button" onclick="somelistener(this)" />';