실패? 이 Working Demo
$(function() {
$("#empty").click(function() {
alert($('#myDiv').is(':empty'));
});
$("#append").click(function() {
$('#myDiv').append('<span>Appended!</span>');
});
});
<!-- not all html shown, for brevity -->
<body>
<div id="myDiv"></div><br/>
<button id="empty">Check if div empty</button>
<button id="append">Append to div</button>
</body>
편집에서 입증 된 바와 같이
그것은 나를 위해 확인 일하고 :
:empty
를 사용의 문제는 또한 요소가 텍스트 노드뿐만 아니라 요소가 포함되어 있는지 여부를 평가한다는 것입니다 텍스트 노드를 포함하는 경우 비어있는 것으로 간주되지 않습니다. 나는 이것이 당신이 원하는 행동이라고 생각하지 않으므로 을 자식 요소의 래핑 된 집합에 사용하여 <td>
에 자식 요소 노드가 있는지 확인해야합니다. 길이가 0 인 경우이 목적으로 비어있는 것으로 간주됩니다 (.length
을 확인하는 것은 .length
을 직접 호출하는 것이 약간 더 빠름을 제외하고 .length
을 호출하는 .size()
을 사용하는 것과 같습니다).
그래서, 여기 당신에게 예를 보여주기 위해 Working Demo의 우리가 this
$('td', this).each(function() {
// this inside the function will reference the <td>
// get any child elements and check the length
if ($('*',this).length === 0) {
// this <td> is empty!
// break out of the each loop
return false;
}
});
참조하는 <tr>
이의 말을 보자. /을 URL에 추가하여 코드를 확인하고 재생하십시오.
jQuery를 코드
<div>
<div class="imagePicker">
<input type="radio" name="imagePicker" />
<img src="http://www.website-designs.com/images/resources_1/icons/Dead.gif" alt="image" />
</div>
<div class="imagePicker" >
<input type="radio" name="imagePicker" />
<img src="http://www.website-designs.com/images/resources_1/icons/Dead2.gif" alt="image" />
</div>
<div class="imagePicker" >
<input type="radio" name="imagePicker" />
<img src="http://www.website-designs.com/images/resources_1/icons/Diablo.gif" alt="image" />
</div>
<div class="imagePicker" >
<input type="radio" name="imagePicker" />
<img src="http://www.website-designs.com/images/resources_1/icons/Ckicken.gif" alt="image" />
</div>
</div>
<br/><br/>
<span id="comment"></span>
<br/>
<table style="margin:10px;">
<thead>
<tr>
<th></th>
<th>Image 1</th>
<th>Image 2</th>
<th>Image 3</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="#" class="select">Select</a></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td><a href="#" class="select">Select</a></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td><a href="#" class="select">Select</a></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
코드를 게시 하시겠습니까? –
명확성을 위해 $ (selector) .is (': empty') 구문을 사용하고 있습니다. 너 수표? – RwwL
if ($ ('# issueimage1'+ issueid) .is (': empty')) { $ ('# issueimage1'+ issueid).추가 (응답 텍스트); } else if ($ ('# issueimage2'+ issueid) .is (': empty')) { $ ('# issueimage2'+ issueid) .append (responseText); } else { $ ('# issueimage3'+ issueid) .append (responseText); } 아이디어는 사용자가 최대 3 장의 사진을 추가 할 수 있다는 것입니다. 각 그림에 대한 전용 요소가 있습니다. 각 요소를 순차적으로 검사하여 삽입 할 위치를 확인합니다. 이것은 처음에는 잘 작동합니다 (페이지로드시 두 장의 사진이 있으면 세 번째 슬롯에 삽입됩니다). 페이지를 새로 고치지 않고 두 이미지를 추가하면 오류가 발생합니다. – Jacob