我收到此函數的錯誤消息,告訴我parseInt沒有radix參數,即使我將它傳遞給變量moveCount。我認爲這意味着id爲#moveCount的跨度值沒有被分配給變量moveCount。我在下面標出了我認爲問題出現的地方。我試過這條線jQuery:選擇具有特定ID的跨度
moveCount = $('span #moveCount').html();
無論在選擇器中是否使用單詞「span」,我都會得到相同的結果。
function incrementMoveCount() {
//gets the html of the span with id
//moveCount
//turns it into a number
//increments it by one
//sets the html of the span with id moveCount
//to the new move count
var moveCount = 0;
var newMoveCount = 0;
moveCount = $('span #moveCount').html(); # problem here
newMoveCount = parseInt(moveCount);
newMoveCount = newMoveCount + 1;
$('span #moveCount').html('newMoveCount');
}
HTML
<div id="meta">
<h1>Checkers</h1>
<h4>Moves: <span id="moveCount">0</span></h4>
</div>
ID的應該是唯一的,但事實並非總是如此。如果您不確定,最好在選擇器中保留span元素。 – Dcortez
@Dcortez雖然不是一個好的做法。根據規範,ID在頁面上應該始終是唯一的https://html.spec.whatwg.org/multipage/dom.html#the-id-attribute –