如何使用setAttribute更新对象中的一个值? [英] How do I update one value within an object using setAttribute?

查看:200
本文介绍了如何使用setAttribute更新对象中的一个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个元素,其中包含一个称为声音的属性。此声音属性的值是一个对象。此对象的属性之一是体积。

I have an element that contains an attribute called "sound". The value of this sound attribute is an object. One of the properties in this object is "volume".

换句话说:

<a-entity position='0 0 0' sound='src: url; on: click; volume: 2.0;'></a-entity>

所以是这样的:

{
  position: '0 0 0',
  sound: {
    src: 'url',
    on: 'click',
    volume: '2.0'
  }
}

我正在使用setAttribute可以更新volume属性,但是我不知道是否有一种有效的方法来仅将一个属性作为目标,而不更改对象中的其他属性。

I'm using setAttribute to update the volume attribute but I don't know if there's an effective way to only target one property while not changing the other properties in the object.

基本上,这种替换声音对象并删除所有未指定的属性(删除了属性的src AND):

Basically this replaces the sound object and removes all the properties that are not specified (src AND on properties are removed):

element.setAttribute('sound', {
  volume: '4'
});

我不想替换整个对象。我只想为一个属性替换一个值。

I don't want to replace the entire object. I only want to replace one value for one property. Is there a best way to go about this?

推荐答案

您可以使用setAttribute的3参数版本,该版本的此处的文档: https://aframe.io/docs /core/entity.html#Updating-Multi-Property-Component-Data

You can make use of the 3-argument version of setAttribute which is in the docs here: https://aframe.io/docs/core/entity.html#Updating-Multi-Property-Component-Data

具体来说,您可以执行以下操作:

Specifically you can do something like:

element.setAttribute("sound", "volume", "4");

这篇关于如何使用setAttribute更新对象中的一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆