使用java脚本显示或隐藏html内容

时间:2015-07-14 12:11:37

标签: javascript jquery html css

我想用java脚本if (condition)显示或隐藏html 希望能够在用户使用Firefox时隐藏html,并在使用其他浏览器时显示内容。



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <script type="text/javascript">
var nVer = navigator.appVersion;
var nAgt = navigator.userAgent;
var browserName  = navigator.appName;
var fullVersion  = ''+parseFloat(navigator.appVersion); 
var majorVersion = parseInt(navigator.appVersion,10);
var nameOffset,verOffset,ix;

// In Opera, the true version is after "Opera" or after "Version"
if ((verOffset=nAgt.indexOf("Opera"))!=-1) {
 browserName = "Opera";
 fullVersion = nAgt.substring(verOffset+6);
 if ((verOffset=nAgt.indexOf("Version"))!=-1) 
   fullVersion = nAgt.substring(verOffset+8);
}
// In MSIE, the true version is after "MSIE" in userAgent
else if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
 browserName = "Microsoft Internet Explorer";
 fullVersion = nAgt.substring(verOffset+5);
}
// In Chrome, the true version is after "Chrome" 
else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {
 browserName = "Chrome";
 fullVersion = nAgt.substring(verOffset+7);
}
// In Safari, the true version is after "Safari" or after "Version" 
else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {
 browserName = "Safari";
 fullVersion = nAgt.substring(verOffset+7);
 if ((verOffset=nAgt.indexOf("Version"))!=-1) 
   fullVersion = nAgt.substring(verOffset+8);
}
// In Firefox, the true version is after "Firefox" 
else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {
 browserName = "Firefox";
 fullVersion = nAgt.substring(verOffset+8);
}
// In most other browsers, "name/version" is at the end of userAgent 
else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) < 
          (verOffset=nAgt.lastIndexOf('/')) ) 
{
 browserName = nAgt.substring(nameOffset,verOffset);
 fullVersion = nAgt.substring(verOffset+1);
 if (browserName.toLowerCase()==browserName.toUpperCase()) {
  browserName = navigator.appName;
 }
}
// trim the fullVersion string at semicolon/space if present
if ((ix=fullVersion.indexOf(";"))!=-1)
   fullVersion=fullVersion.substring(0,ix);
if ((ix=fullVersion.indexOf(" "))!=-1)
   fullVersion=fullVersion.substring(0,ix);

majorVersion = parseInt(''+fullVersion,10);
if (isNaN(majorVersion)) {
 fullVersion  = ''+parseFloat(navigator.appVersion); 
 majorVersion = parseInt(navigator.appVersion,10);
}
 
if( browserName == "Chrome")
	document.write("What should i put at this line to hide test div?"); 
    
else
	document.write("Access Denied!,what should i put at this line to hide test div?"); 

</script>
</head>
<body>
<div id = "test">
    <p>test div</p>
</div>
</body>
</html>
&#13;
&#13;
&#13;

我希望使用if condition显示或隐藏内容。我使用此代码阻止在常见浏览器中打开我的网站,而不是我为我的网站设计的VB浏览器。

3 个答案:

答案 0 :(得分:0)

你可以在jquery中这样做:

if( condition ){
   $( "#test" ).hide();
}else{
   $( "#test" ).show();
}

如果你想用JavaScript做,你可以这样做:

if( condition ){
    document.getElementById("test").style.display="none";
}else{
    document.getElementById("test").style.display="block";
}

答案 1 :(得分:0)

向css添加一个类:

.hidden { visibility:hidden; }

基于if声明:

a = document.getElementById('test')
a.className = a.className + ' hidden'

当然,没有任何代码可以使用javascript被禁用。

答案 2 :(得分:0)

试试这个:

document.getElementById("test").style.display="none";

这将使用id test隐藏元素。

当然,它将在 if-condition 部分中执行,您希望隐藏 div 标记。