var gameData = {
love: 10000,
hairBrushes: 0,
}
function buyHairBrush(id1) {
var hairBrushCost = Math.floor(1 * Math.pow(1.1, gameData.hairBrushes));
if (gameData.love >= hairBrushCost) {
gameData.hairBrushes = gameData.hairBrushes + 1;
gameData.love = gameData.love - hairBrushCost;
document.getElementById(id1).innerHTML = gameData.hairBrushes;
};
var nextCost = Math.floor(1 * Math.pow(1.1, gameData.hairBrushes));
document.getElementById('hairBrushCost').innerHTML = nextCost;
};
<div id='store'>
<button id='buyHairBrushButton' class="productionButton" onClick='buyHairBrush(hairBrushesAmount)'>Hair Brush cost:<span id="hairBrushCost">10</span></button>
</div>
Hair Brushes: <span id="hairBrushesAmount">0</span>
我想使用.getElementById定位函数内部的ID,当使用id本身时,该ID可以很好地工作,但是当将其作为函数参数给出时,它返回null。
我尝试将“'”连接到参数,但似乎不起作用。
这是我要用于执行功能的元素。
<button id='notTheIdIWantToTarget' class="exampleClass" onClick='myFunction(idArgument)'>text<span id="notThisIdEither">text</span></button>
这是功能:
function myFunction(idArgument){ ... ... ... ... document.getElementById(idArgument).innerHTML = ...; ... }; ... ... };
最后是我要定位的元素:
text<span id="IdIWantToTarget">text</span>
我希望函数执行并以“ IdIWantToTarget” id为目标,但我得到的是null。
var gameData = {爱情:10000,hairBrushes:0,}函数buyHairBrush(id1){var hairBrushCost = Math.floor(1 * Math.pow(1.1,gameData.hairBrushes));如果(gameData.love> = ...
该函数不知道id所引用的元素。问题出在将变量分配给id的地方。该代码段仅显示该函数应与function参数一起使用。