如何在js中发送和获取对象作为函数参数

时间:2016-12-17 15:11:52

标签: javascript

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<script>

function insertObject(obj) {
    document.getElementById("demo").innerHTML = obj.arguments[0] + ' ' + obj.arguments[1];
    // So I want to write "John James "
}

</script>

</head>
<body >

<button onclick="insertObject({name: "John", surname: “James”})">Try it</button>

<p id="demo"></p>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

下次请花点时间写出你的问题。

同时,希望这会让你前进

function insertObject(obj) {
    document.getElementById("demo").textContent = obj.name + ' ' + obj.surname;
}
Test
<button onclick='insertObject({name: "John", surname: "James"})'>Try it</button>

<p id="demo"></p>