click 이벤트 핸들러로 함수를 실행하는 데 문제가 있습니다. numberCheck() 함수를 제거하고 코드를 이벤트 핸들러에 직접 게시하면 잘 작동합니다. 여기에 뭔가 기본적인 것이 빠져 있니?함수 호출이 '클릭'이벤트 핸들러와 작동하지 않습니다.
jQuery(document).ready(function(){
var scopedVariable;
var number = Math.floor(Math.random()*11);
function numberCheck() {
$(this).closest('#outsideContainer').find('.hotter').addClass('hideStuff');
$(this).closest('#outsideContainer').find('.colder').addClass('hideStuff');
var guess = $(this).closest('#guessContainer').find('.value').val();
if(+guess === number)
{
$(this).closest('#outsideContainer').find('.gotIt').removeClass('hideStuff');
}
else {
$(this).closest('#guessContainer').find('.value').val('Please guess again');
if(Math.abs(guess - number) < Math.abs(scopedVariable - number)) {
$(this).closest('#outsideContainer').find('.hotter').removeClass('hideStuff');
}
else if(Math.abs(guess - number) > Math.abs(scopedVariable - number)) {
$(this).closest('#outsideContainer').find('.colder').removeClass('hideStuff');
}
scopedVariable = guess;
}
}
$('.query').on('click', function() {
numberCheck();
});
});
여기에 필요한 경우 내 HTML이다 : 이것에 대한 어떤 도움이 많이 감사합니다
<body>
<div id="outsideContainer">
<div id="guessContainer">
<h3> Guess a number between 1 and 10 </h3>
<input id="txtfield" name="txtfield" type="text" class="value"/><br>
<input type = "submit" class="query" />
</div>
<div id="guessShow">
<p class="hotter hideStuff" > Getting Hotter!! </p>
<p class="colder hideStuff"> Getting Colder, BRRR!!! </p>
<p class="gotIt hideStuff"> Awesome! You have guessed the number </p>
</div>
</div>
</body>
여기 내 코드입니다.
나는 새로운 함수 안에서'$ (this)'이 범위를 벗어난다. – Rick
프레임 워크를 사용할 때 프레임 워크를 언급하는 것을 잊지 마십시오. (correct) – Sebas
그래,'$ (this)'를 HTML의 실제 요소로 대체하거나 매개 변수로 전달하십시오. –