2017-09-29 68 views
0

我正在開發一款針對android互聯網瀏覽器的測試項目。我正在使用JavaScript隱藏一些對象onLoad。在Android網絡瀏覽器中隱藏HTML元素

問題是,它是在Mozilla Firefox中工作,但不工作在Android默認的互聯網瀏覽器。

這裏是我的全碼:

<!DOCTYPE html> 
<html> 

<script type="text/javascript" async> 

function ajaxpath_59c479660b217(url){return window.location.href == '' ? url : url.replace('&s=','&s=' + escape(window.location.href));}(function(){document.write('<div id="fcs_div_59c479660b217"></div>');fcs_59c479660b217=document.createElement('script');fcs_59c479660b217.type="text/javascript";fcs_59c479660b217.src=ajaxpath_59c479660b217((document.location.protocol=="https:"?"https:":"http:")+"//www.freecommentscript.com/GetComments2.php?p=59c479660b217&s=#!59c479660b217");setTimeout("document.getElementById('fcs_div_59c479660b217').appendChild(fcs_59c479660b217)",1);})(); 

    function myFunction() { 
    document.getElementById("comment-ad").style.display = "none"; 
} 

function myFunction2() // no ';' here 
{ 
    var elem = document.getElementById("fcs_Comment_59c479660b217"); 
    if (elem.value=="Enter your comment here") elem.value = "Enter your post here"; 
    else elem.value = "Enter your post here"; 
} 

function myFunction3() // no ';' here 
{ 
     document.getElementById("comment-rss-feed").style.display = "none"; 

} 

function showFilterItem() { 
    if (filterstatus == 0) { 
     filterstatus = 1; 
     $find('<%=FileAdminRadGrid.ClientID %>').get_masterTableView().showFilterItem(); 
     document.getElementByClassName("ShowButton").innerHTML = "Hide Filter"; 
    } 
    else { 
     filterstatus = 0; 
     $find('<%=FileAdminRadGrid.ClientID %>').get_masterTableView().hideFilterItem(); 
     document.getElementById("ShowButton").innerHTML = "Show filter"; 
    } 
} 


if (document.readyState === "complete") { 
    myFunction(); 
    myFunction2(); 
    myFunction3(); 
    showFilterItem(); 
} 
else { 
    window.onload = function() { 
     myFunction(); 
     myFunction2(); 
     myFunction3(); 
     showFilterItem(); 
    }; 
}; 

</script> 


</body> 
</html> 
+0

腳本是在你想要隱藏的HTML元素之前添加的,還是在它們之後? –

+0

我只是使用我提供的其他東西的腳本 – Arman

回答

0

我把整個腳本中domloaded事件,以確保JS可以找到DOM元素。像這樣:

<script type="text/javascript" async> 
document.addEventListener('DOMContentLoaded', function(){ 
function ajaxpath_59c479660b217(url){return window.location.href == '' ? url : url.replace('&s=','&s=' + escape(window.location.href));}(function(){document.write('<div id="fcs_div_59c479660b217"></div>');fcs_59c479660b217=document.createElement('script');fcs_59c479660b217.type="text/javascript";fcs_59c479660b217.src=ajaxpath_59c479660b217((document.location.protocol=="https:"?"https:":"http:")+"//www.freecommentscript.com/GetComments2.php?p=59c479660b217&s=#!59c479660b217");setTimeout("document.getElementById('fcs_div_59c479660b217').appendChild(fcs_59c479660b217)",1);})(); 

    function myFunction() { 
    document.getElementById("comment-ad").style.display = "none"; 
} 

function myFunction2() // no ';' here 
{ 
    var elem = document.getElementById("fcs_Comment_59c479660b217"); 
    if (elem.value=="Enter your comment here") elem.value = "Enter your post here"; 
    else elem.value = "Enter your post here"; 
} 

function myFunction3() // no ';' here 
{ 
     document.getElementById("comment-rss-feed").style.display = "none"; 

} 

function showFilterItem() { 
    if (filterstatus == 0) { 
     filterstatus = 1; 
     $find('<%=FileAdminRadGrid.ClientID %>').get_masterTableView().showFilterItem(); 
     document.getElementByClassName("ShowButton").innerHTML = "Hide Filter"; 
    } 
    else { 
     filterstatus = 0; 
     $find('<%=FileAdminRadGrid.ClientID %>').get_masterTableView().hideFilterItem(); 
     document.getElementById("ShowButton").innerHTML = "Show filter"; 
    } 
} 


if (document.readyState === "complete") { 
    myFunction(); 
    myFunction2(); 
    myFunction3(); 
    showFilterItem(); 
} 
else { 
    window.onload = function() { 
     myFunction(); 
     myFunction2(); 
     myFunction3(); 
     showFilterItem(); 
    }; 
}; 
}); 
</script> 
+0

我是新的DOM。你能舉個例子說明你的建議,如果可能的話? – Arman

+0

當您嘗試處理腳本中的某些DOM元素(即document.getElementByID('xxx'))時,JavaScript必須找到該元素才能執行與其相關的代碼。如果Javascript找不到這個元素(因爲它沒有準備好在dom中呈現),就會出現問題。爲了解決這個問題,你可以在我的帖子中使用Event監聽器,只是爲了確保所有的DOM元素都被加載並且可以訪問javascript –