Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
根据Javascript函数输出显示不同的链接_Javascript_Html - Fatal编程技术网

根据Javascript函数输出显示不同的链接

根据Javascript函数输出显示不同的链接,javascript,html,Javascript,Html,我试图根据用户的不同在页面上显示不同的链接。例如,移动用户不允许向我们的数据库提交数据。我发现了一个检测移动用户的Javascript函数,但我无法让它的输出指示显示给用户的链接。事实上,当我尝试使用以下代码时,根本没有显示任何链接 </head> <body> </div> <div id="topcontent"> <p align="center"> <div id="bann

我试图根据用户的不同在页面上显示不同的链接。例如,移动用户不允许向我们的数据库提交数据。我发现了一个检测移动用户的Javascript函数,但我无法让它的输出指示显示给用户的链接。事实上,当我尝试使用以下代码时,根本没有显示任何链接

</head>
  <body>
    </div>
    <div id="topcontent">
      <p align="center">
        <div id="banner">
          <p align="center">
            <script>
            function checkMobile(){
              if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
                <a href="studyAreas.html">
                Wifi Study Areas</a>
                <a href="displayspeeds.html">
                  View all Speeds</a>
                  <a href="bestlocation.html">
                    Best Speed Near You</a>
                        <a href="squadfree/index.html">
                        About Us</a>
              }
              else{
                <a href="studyAreas.html">
                Wifi Study Areas</a>
                <a href="displayspeeds.html">
                  View all Speeds</a>
                  <a href="bestlocation.html">
                    Best Speed Near You</a>
                    <a href="userEnterInfo.html">
                      Add Speed Data</a>
                        <a href="squadfree/index.html">
                        About Us</a>
              }
            }
            </script>
          </p>
      </div>

      <div id="centercontent">
        <h3 align="center">
          Find the best study space</h3>
          <h1 align="center">
            Check Your Internet Speed With WiFly</h1>
            <p align="center">
              <img src="http://cdn.flaticon.com/png/256/15402.png" alt="wifi" />
            </p>
            <p align="center">
            </p>
      </div>
  </body>

函数checkMobile(){ if(/Android | webOS | iPhone | iPad | iPod | BlackBerry | IEMobile | Opera Mini/i.test(navigator.userAgent)){ } 否则{ } }

寻找最好的学习空间 用WiFly检查你的网速


您不能像这样在javascript中直接使用html代码

<p id="content" align="center">

</p>

<script>
window.onload = function() {

        if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
            document.getElementById("content").innerHtml = '<a href="studyAreas.html">...';
        }
        else{
            document.getElementById("content").innerHtml = '<a href="studyAreas.html">...';
        }

}
</script>

window.onload=函数(){ if(/Android | webOS | iPhone | iPad | iPod | BlackBerry | IEMobile | Opera Mini/i.test(navigator.userAgent)){ document.getElementById(“content”).innerHtml='…'; } 否则{ document.getElementById(“content”).innerHtml='…'; } }
这将遵循您当前的方法。如果您只需在两个块中创建元素并在所需的块中淡入淡出,可能会更好(更容易)

<p id="content" align="center">
   <span id="mobile">...</span>

   <span id="desktop">...</span>
</p>

<script>

window.onload = function() {

        if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
            document.getElementById("desktop").style.display = 'none';
            document.getElementById("mobile").style.display = 'block';
        }
        else{
            document.getElementById("desktop").style.display = 'block';
            document.getElementById("mobile").style.display = 'none';
        }

}
</script>

... ...

window.onload=函数(){ if(/Android | webOS | iPhone | iPad | iPod | BlackBerry | IEMobile | Opera Mini/i.test(navigator.userAgent)){ document.getElementById(“桌面”).style.display='none'; document.getElementById(“mobile”).style.display='block'; } 否则{ document.getElementById(“桌面”).style.display='block'; document.getElementById(“mobile”).style.display='none'; } }
如果您使用jquery并只切换元素,那么这将更加容易

重要提示:这样您就无法确保人们不会将数据发布到您的数据库中。您还应该在后端检查此项


更新:正如阿南德在评论中所说,你也应该重新思考你的拳击。不能在段落内添加div。只有在直接向段落添加文本时才应使用段落。

不能直接在脚本标记内呈现HTML。此语法在ASP.NET Rajor中是允许的,但不适用于JavaScript

工作演示

您应该根据条件创建HTML字符串,然后将该字符串指定为周围容器的
innerHTML
。为了获得更好的性能,您可以为容器提供唯一的
id

HTML:

 <p align="center" id="linkContainer">
 </p>

JavaScript:

function checkMobile(){
                var strHtml="";
              if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
                strHtml='<a href="studyAreas.html">'
                        + 'Wifi Study Areas</a>'
                        + '<a href="displayspeeds.html">'
                        + 'View all Speeds</a>'
                        + '<a href="bestlocation.html">'
                        + 'Best Speed Near You</a>'
                        + '<a href="squadfree/index.html">'
                        + 'About Us</a>';
              }
              else{
               strHtml= '<a href="studyAreas.html">'
                        + 'Wifi Study Areas</a>'
                        + '<a href="displayspeeds.html">'
                        + 'View all Speeds</a>'
                        + '<a href="bestlocation.html">'
                        + 'Best Speed Near You</a>'
                        + '<a href="userEnterInfo.html">'
                        + 'Add Speed Data</a>'
                        + '<a href="squadfree/index.html">'
                        + 'About Us</a>';
              }

               document.getElementById("linkContainer").innerHTML=strHtml;
            }

//finally call the function.
checkMobile();
函数checkMobile(){
var strHtml=“”;
if(/Android | webOS | iPhone | iPad | iPod | BlackBerry | IEMobile | Opera Mini/i.test(navigator.userAgent)){
strHtml=''
+ ''
+ ''
+ '';
}
否则{
strHtml=''
+ ''
+ ''
+ ''
+ '';
}
document.getElementById(“linkContainer”).innerHTML=strHtml;
}
//最后调用函数。
checkMobile();

您不能只在javascript函数中编写html代码。例如,您可以淡入正确的html部分,或者用代码(set innerhtml)填充必要的元素@Fank Provost所说的是正确的,除此之外,为什么要在段落标记中添加div标记?您声明
var html
,但将html分配给全局
strHtml
。可能是打字错误。