首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用document.getElementbyID('').innerHTML将文本从一个函数链接到同一个网页?

如何使用document.getElementbyID('').innerHTML将文本从一个函数链接到同一个网页?
EN

Stack Overflow用户
提问于 2014-04-18 01:04:56
回答 3查看 104关注 0票数 0

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

代码语言:javascript
复制
<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>
EN

回答 3

Stack Overflow用户

发布于 2014-04-18 01:13:04

您的代码有几个问题。

当用户单击p

时,您正在将innerHTML设置为空字符串,而不是下一行上的字符串

  • 您将document.getElementById错误键入为

未将处理程序设置为调用代码

这是一个工作版本的http://jsfiddle.net/mendesjuan/Ljf28/1/

代码语言:javascript
复制
// I added an ID to the p, so you can hookup the handler from JS
document.getElementById('clickme').addEventListener('click', function(){
    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";    
});

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

另外,

票数 0
EN

Stack Overflow用户

发布于 2014-04-18 01:16:28

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

你已经很接近了。你只需要在页面上添加一个链接就可以了。

代码语言:javascript
复制
<a href="#" onclick="functionName()">Click here</a>...

http://jsfiddle.net/NUqdZ/

根据评论编辑

其他错误,我没有提及而改正...

  • innerHTML语法不正确。应该是:在两者之间不带分号的innerHTML="INSERT TEXT HERE"。这将结束语句,并且不会将文本插入到getElementByID

中。

  • 更新了jsfiddle,以反映以下建议的更改:

  • document.getElementById

  • div
票数 0
EN

Stack Overflow用户

发布于 2014-04-18 01:27:15

很多东西都不见了。这里是byId的byID。“D”应该很小,并且那里没有onclick事件。下面是代码

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23139247

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档