25

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

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

显然,这是行不通的。有人有什么想法吗?谢谢!

@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>
4

5 回答 5

25

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

当您在 中拥有元素的实例时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);
}

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

于 2012-08-24T12:56:56.657 回答
9
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);
                  }
于 2017-12-20T08:32:21.207 回答
3

只需使用 JSON.strigify(obj) 对对象进行字符串化,并将其作为参数传递给 onclick 函数,它将被直接解析并作为对象在函数中接收。

function myfunction(obj,parentobj){ 
   var o=document.createElement("div");
   var stringifiedObj = JSON.stringfy(obj)

   o.innerHTML='<input type="button" onclick=`somelistener(${stringifiedObj})` />';
   parentobj.appendChild(o);
}

function somelistener(obj){
   console.log(typeof obj) //Object
}
于 2021-02-14T18:03:23.600 回答
3

老问题的答案我不太明白,但最简单的解决方案是结合函数encodeURIComponentJSON.stringify,即您将对象字符串化,然后转义那些特殊字符,然后在函数内部反转过程

var htmlStr = `<button onclick="myFunctions('${encodeURIComponent(JSON.stringify(obj))}')"></button>`

function myFunctions(obj) {
  obj = JSON.parse(decodeURIComponent(obj))
  // do something with obj
}

在这里测试:

var obj = {a: 'hey there', b: 1}

var htmlStr = `<button onclick="myFunctions('${encodeURIComponent(JSON.stringify(obj))}')">Click me</button>`

$('#mydiv').append(htmlStr)

function myFunctions(obj) {
  obj = JSON.parse(decodeURIComponent(obj))
  console.log(obj.a)
  console.log(obj.b)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="mydiv"></div>

于 2021-03-22T11:57:50.320 回答
-8

像这样通过它:

o.innerHTML='<input type="button" onclick="somelistener(this)" />';
于 2013-11-18T06:08:22.087 回答