正在尝试将此信息链接到“单击此处查看..”并将其显示在同一页上
<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>发布于 2014-04-18 01:13:04
您的代码有几个问题。
当用户单击p
时,您正在将innerHTML设置为空字符串,而不是下一行上的字符串
document.getElementById错误键入为未将处理程序设置为调用代码
这是一个工作版本的http://jsfiddle.net/mendesjuan/Ljf28/1/
// 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()。
另外,
发布于 2014-04-18 01:16:28
因此,如果我理解正确的话,当您单击单击此处部分时,您希望该文本显示在页面的"defArea“部分。
你已经很接近了。你只需要在页面上添加一个链接就可以了。
<a href="#" onclick="functionName()">Click here</a>...http://jsfiddle.net/NUqdZ/
根据评论编辑:
其他错误,我没有提及而改正...
innerHTML="INSERT TEXT HERE"。这将结束语句,并且不会将文本插入到getElementByID中。
发布于 2014-04-18 01:27:15
很多东西都不见了。这里是byId的byID。“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>https://stackoverflow.com/questions/23139247
复制相似问题