gpt4 book ai didi

javascript - JS传递一个对象属性作为函数的参数

转载 作者:行者123 更新时间:2023-11-30 09:44:32 27 4
gpt4 key购买 nike

<分区>

我无法将对象属性 fullName 作为函数 setData 的参数传递。

函数 setData 应在模态窗口中输入某个值后调用。

我想使用函数 setDatavalue 设置为 user.fullName

对象示例 user = { fullName: "Full Name", position: "Manager"}

function setData ( data ) {
var user,
value = prompt("Value", "");

if ( localStorage.getItem("user") ) {
user = JSON.parse( localStorage.getItem("user") );
}

user.data = value; // user.fullName = value; - as an example

if ( value ) {
localStorage.setItem("user", JSON.stringify( user ) );
location.reload();
}
}

document.getElementById("full-name-button").onclick = function() {
setData( "fullName" );
};

document.getElementById("position").onclick = function() {
setData( "position" );
};

帮助我,如何传递对象属性,因为我不知道该怎么做。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com