2011-02-01 14 views

Odpowiedz

13
if(document.getElementById("divid")!=null){ 
    alert('Div exists') 
} 
1

Jak to:

<script type="text/javascript"> 
function CheckExists() { 
    var oDiv = document.getElementById("turtles"); 
    if (oDiv) { 
    alert("exists"); 
    } 
    else { 
    alert("does not exist"); 
    } 
} 
</script> 

Ta funkcja musi znajdować się na dole strony lub wywoływana po zakończeniu ładowania strony.

2

jeśli masz identyfikator div, można zrobić to w ten sposób:

var myDiv = document.getElementById('turtles'); 

if (myDiv) { 
    //It exists 
} 

overwise, jeśli jest to klasa, lepiej użyć ram (jQuery tutaj):

if ($('.turtles').length > 0) { 
    //it exists 
} 
1

Chciałbym tylko wskazać, że document.contains jest innym sposobem, aby to zrobić.

document.contains jest szczególnie przydatny, jeśli masz aplikację internetową, której komponenty są renderowane praktycznie przed wstawieniem do DOM.

Powiązane problemy