버튼을 클릭하면 팀 구성원 목록에서 선장을 선택할 수있는 코드를 작성하려고합니다. 버튼을 클릭하면 가능한 팀 구성원 (테이블에 배치)이 색을 변경 한 다음 그 중 하나를 클릭하면 새로운 선장으로 지정됩니다. 화면의 다른 곳을 클릭하면 팀 구성원이 정상적인 색으로 돌아가고 페이지가 정상으로 되돌아 가도록하고 싶습니다.jquery의 if/else 문으로 혼동 됨
Here은 내 시도를위한 피들입니다. 보시다시피, 작동하지 않습니다.
<table>
<tr>
<td class="name">John Smith</td><td class="captain"></td>
</tr>
<tr>
<td class="name">Smith Johnson</td><td class="captain"></td>
</tr>
<tr>
<td class="name">Joth Smithson</td><td class="captain"></td>
</tr>
</table>
<div id="button">C</div>
그리고 내 JS는 다음과 같습니다 :
내 HTML은
$('#button').click(function() {
var people = $('table tr')
people.css('background-color','red');
people.find('td.captain').html('');
$('html').click(function(event) {
var target = $(event.target);
if($('table tr').has(target).length) {
var newcaptain = $('table tr').has(target);
newcaptain.find('td.captain').html('C');
people.css('background-color','');
$('html').off('click');
} else {
people.css('background-color','');
$('html').off('click');
}
});
});
정말로 나를 당혹 것은 내가 다른 절에서 문을 제거하면 경우 부분은 I로 작동한다는 것입니다 그것을 의도했다. 이것이 어떻게 가능한지?
조건부의 양쪽에 동일한 코드 줄을 넣는다면,이 줄은 처음부터 조건부 일 필요는 없습니다. 즉,'people.css ('background-color', ''); $ ('html'). off ('click');''if '결과에 상관없이 항상 실행됩니다. 무의미한. – Sparky
예, 공정한 의견, 감사합니다. – lac