2

例如:

function getInfo(record){    //  record is an object
    ...
}

这个函数可以被 onclick 事件使用。但是,当浏览器解析它时,它会被解析为一个字符串。所以 Firebug 在元素列表之后显示“缺少 ]”。

有人可以解决这个问题吗?

4

3 回答 3

1

you mean something like this:

somefunction = function(record)
{
    console.log('record: ' + record);
}

this.onclick = somefunction.bind(this, 100);

here 100 is argument you are passing to "somefunction", and it will print 100 on console.

I hope this will help.

html code can be something like:

<html>
    <head>
        <title> test page </title>
        <script>
            somefunction = function(record)
            {
                alert("record: " + record);
                var obj = eval('(' + record + ')');
                alert(" a:" + obj.a + " b: " + obj.b);
            }
        </script>
    </head>
    <body>
        <input type="button" name="btn1" value="Button1" 
               onclick="somefunction('{ a: 100, b: 200 }')">
    </body>
</html>
于 2012-07-22T11:29:30.160 回答
1

If Your's onclick is in HTML then try like this,

<input type="button"  onclick="getInfo(this)"  value="Click" />

You get the DOM Object

于 2012-07-22T11:33:36.790 回答
1

或者,如果您对对象类型非常好奇,您可以将对象存储在单独的变量中。

<html>
<head>
    <title> test page </title>
    <script>
        somefunction = function(record)
        {
            alert("record: " + record);
            var obj = eval('(' + record + ')');
            alert(" name:" + obj.name + " mail: " + obj.mail);
        }
    </script>
</head>
<body>
    <script>
        var myJSON = "{name:'john', mail:'aa@aa.com'}";
    </script>
    <input type="button" name="btn1" value="Button1" 
           onclick="somefunction(myJSON)">
</body>

于 2012-07-22T12:55:12.130 回答