Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.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 如何使用document.getElementbyID(';';).innerHTML将函数中的文本链接到同一网页?_Javascript_Html_Function_Hyperlink_Innerhtml - Fatal编程技术网

Javascript 如何使用document.getElementbyID(';';).innerHTML将函数中的文本链接到同一网页?

Javascript 如何使用document.getElementbyID(';';).innerHTML将函数中的文本链接到同一网页?,javascript,html,function,hyperlink,innerhtml,Javascript,Html,Function,Hyperlink,Innerhtml,正在尝试将此信息链接到“单击此处查看…”并将其显示在同一页面上 <script type="text/javascript"> function rhinoinfo(){ document.getElementByID('defArea').innerHTML=""; "There are five different species of rhinoceros. The name rhinoceros means ‘nose horn’ and is

正在尝试将此信息链接到“单击此处查看…”并将其显示在同一页面上

<script type="text/javascript">
  function rhinoinfo(){
    document.getElementByID('defArea').innerHTML="";
    "There are five different species of rhinoceros. The name rhinoceros means  
    ‘nose horn’ and is often shortened to rhino. It comes from the Greek words  
    rhino (nose) and ceros (horn).White rhinoceros are the second largest land  
    mammal. Rhinos can grow to over 6 feet tall and more than 11 feet in length.  
    Rhinoceros have thick, sensitive skin. Source: Savetherhino.org";
  }
</script>


<p>Click here to see information about the rhino.</p>
</div>
<div id="defArea">
  <p></p>
</div>
</body>
</html>

函数rhinoinfo(){
document.getElementByID('defArea').innerHTML=“”;
犀牛有五种不同的种类。犀牛这个名字的意思是
“鼻角”,通常缩写为rhino。它来自希腊语
犀牛(鼻子)和犀牛(角)。白犀牛是第二大陆地
哺乳动物。犀牛可以长到6英尺高,11英尺长。
犀牛有厚而敏感的皮肤;
}
单击此处查看有关犀牛的信息


您的代码有一些问题

  • 您将innerHTML设置为空字符串,而不是下一行的字符串
  • 您将
    document.getElementById
    误输入为
    document.getElementById
  • 当用户单击p标记时,您没有设置调用代码的处理程序
这是一个有效的版本

您将innerHTML设置为空字符串,而不是下一行的字符串。当然,当用户单击p时,还必须调用
rhinoinfo()


另外,

如果我理解正确,当您单击“单击此处”部分时,您希望该文本显示在页面的“defArea”部分

你很接近。你只需要在页面上添加一个链接就可以做到这一点

<a href="#" onclick="functionName()">Click here</a>...
。。。

根据评论编辑

我纠正的其他错误没有提及

  • innerHTML语法不正确。应该是:
    innerHTML=“在此处插入文本”
    ,两者之间不带分号。这将结束语句,而不是将文本插入div
  • 更新了JSFIDLE以反映下面建议的更改
  • 它的document.getElementById不是getElementById

    • 丢失了一些东西。 我是拜伊德,拜伊德。”d'应该很小,并且没有onclick事件。下面是代码

      <html>
      <script type="text/javascript">
      function rhinoinfo(){
          document.getElementById('defArea').innerHTML = 
              "<P>There are five different species of rhinoceros. The name rhinoceros means  " +
              "'nose horn' and is often shortened to rhino. It comes from the Greek words " +
          "rhino (nose) and ceros (horn).White rhinoceros are the second largest land  " +
          "mammal. Rhinos can grow to over 6 feet tall and more than 11 feet in length.  " +
          "Rhinoceros have thick, sensitive skin. Source: Savetherhino.org</P>";
       }
      </script>
      
      <body>
      <p onclick="javascript:rhinoinfo();">Click here to see information about the rhino.</p>
      </div>
      <div id="defArea">
      </div>
      </body>
      </html>
      
      
      函数rhinoinfo(){
      document.getElementById('defArea')。innerHTML=
      “犀牛有五种不同的种类。犀牛这个名字的意思是”+
      “鼻角”,通常缩写为rhino。它来自希腊语”+
      “犀牛(鼻子)和犀牛(角)。白犀牛是第二大陆地”+
      “哺乳动物。犀牛可以长到6英尺高,11英尺长。”+
      “犀牛有厚而敏感的皮肤。来源:Savetherhino.org

      ”; } 单击此处查看有关rhino的信息


      请同时提供您的代码演示。如果您要使用链接,它应该有一个真正的
      href
      ,以便在禁用JS时工作。您还应该返回false,以便不更新URL片段。最后,您不应该向noob建议内联处理程序,而是教他们一种更好的方法。还有其他你没有提到的错误。
      <html>
      <script type="text/javascript">
      function rhinoinfo(){
          document.getElementById('defArea').innerHTML = 
              "<P>There are five different species of rhinoceros. The name rhinoceros means  " +
              "'nose horn' and is often shortened to rhino. It comes from the Greek words " +
          "rhino (nose) and ceros (horn).White rhinoceros are the second largest land  " +
          "mammal. Rhinos can grow to over 6 feet tall and more than 11 feet in length.  " +
          "Rhinoceros have thick, sensitive skin. Source: Savetherhino.org</P>";
       }
      </script>
      
      <body>
      <p onclick="javascript:rhinoinfo();">Click here to see information about the rhino.</p>
      </div>
      <div id="defArea">
      </div>
      </body>
      </html>