我总是能够通过寻址它的 ID 来更改 img 元素的 src。首先,我定义了 id 为“lol”的元素。
<img id="lol" src="" />
然后我单击一个按钮来激活此功能,这会更改 src:
<script> document.getElementById('lol').setAttribute('src', `data.value[0].thumbnailUrl); </script>`
现在,虽然这对于 img 标签来说非常有效,但对于我的 a-box 元素却不起作用:
<a-box id="lol" src="flammer.png" position="-10 0.5 1" rotation="0 45 0" width="1" height="1" depth="1"></a-box>
感谢任何帮助,谢谢。
最佳答案
经过一些调整后对我有用。在 Chrome 中运行它。
<!DOCTYPE HTML>
<html>
<head>
</head>
<body >
<p>Open inspector and look at src parameter of a-box which will be boo.png</p>
<a-box id="lol" src="flammer.png" position="-10 0.5 1" rotation="0 45 0" width="1" height="1" depth="1"></a-box>
</body>
<script>
var data = {value:[{thumbnailUrl:"boo.png"}, {thumbnailUrl:"blah.png"}]}
alert('Before: Src or a-box=' + document.getElementById("lol").getAttribute("src"))
document.getElementById("lol").setAttribute("src", data.value[0].thumbnailUrl);
alert('After: Src or a-box=' + document.getElementById("lol").getAttribute("src"))
</script>
</html>
关于javascript - 使用 JavaScript 更改 <a-box> 的 src,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40483247/