2017-02-11 3 views
4

에 의미합니까 내가이 내 HTML 페이지 내부의 비어있는 사업부 :빈 DIV JQuery와 또는 자바 스크립트

나는 동적으로이 HTML을 div에,하지만 그 전에, 내가 볼 필요가 업데이트해야
<div id='onlineaccess' style='width:20em;'> 
</div> 

비어있는 경우. 나는이에 대해 다음 JQuery와 자바 스크립트 코드를 작성했습니다 :

if($('#onlineaccess').is(':empty')) 
{alert('No HTML inside of onlineaccess div');} 
else 
{alert('Some HTML inside of onlineaccess div');} 

그러나 이것은 내가 사업부가 비어있는 경우를 찾고 있어요 결과를 생성하지 않습니다.

if($('#onlineaccess').is(':empty')) 
 
{alert('No HTML inside of onlineaccess div');} 
 
else 
 
{alert('Some HTML inside of onlineaccess div');}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id='onlineaccess' style='width:20em;'> 
 
    </div>

이 사업부가 비어있는 경우에도 두 번째 메시지를 경고

. 아무도 내게 설명 할 수 있을까요? 왜 그런가요? 그리고 어떻게 첫 번째 메시지를 경고 메시지에 넣을 수 있습니까?

미리 도움을 청하십시오.

+1

[': empty'] (https://api.jquery.com/empty-selector/) : "_Select이 INSEAD를 사용하여 자식이없는 모든 요소 (** 텍스트 노드 ** 포함) _ " – Andreas

+1

'div' *는 비어 있지 않습니다. 공백을 포함합니다. – David

답변

2

https://api.jquery.com/empty-selector/

if($('#onlineaccess:empty')) 
 
{alert('No HTML inside of onlineaccess div');} 
 
else 
 
{alert('Some HTML inside of onlineaccess div');}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id='onlineaccess' style='width:20em;'> 
 
    </div>

0

당신은 너무 그것은 줄 바꿈의

if($.trim($('#onlineaccess').html()) ==='') 
2

해야 아이디의 HTML() 콘텐츠를해야한다. 일부 브라우저는 이것을 다른 브라우저와 다르게 해석합니다.

쓰기 그것은이 방법 :

<div id='onlineaccess' style='width:20em;'></div> 

당신이 의도 한 동작을 얻을.

관련 : How do I check if an HTML element is empty using jQuery?

+1

이 질문을 복제본으로 표시하는 대신 답변의 일부로 복제본을 게시 하시겠습니까? O.o – Andreas

+0

어떻게하는지 모르겠습니다. – mwallisch

0

사업부는 비어 있지 않은 내용으로 간주하는 내부 텍스트 노드를 가지고, 실제로 비어 있지 않습니다.

$(document).ready(function() { 
 
    if ($("#onlineaccess").html() === "") { 
 
    console.log('empty'); 
 
    } else { 
 
    console.log('not empty'); 
 
    } 
 
    
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id='onlineaccess' style='width:20em;'> 
 
</div>

1

var check = !$.trim($('#div').html()).length; 
 

 
if (check) { 
 
    console.log('no html'); 
 
} else { 
 
    console.log('some html'); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 

 
<div id='div' style='width:20em;'> 
 
</div>