javascript - 通过滚动使图像变大,单击 javascript 时显示文本

标签 javascript html hover

我正在尝试通过翻转使图像变大,然后如果单击显示文本,然后再次单击图像并使文本消失。我无法让文本部分工作。我只能通过翻转使图像变大。这是我的 HTML 和 JS 代码。

HTML:

<head>
<script src="picturetask.js"> </script> 
<body>
<h1 style="text-align:center">Picture Manipulation</h1>
<center>
<img onmouseover="bigImg(this)" onmouseout="normalImg(this)" border="0" src="../images/hippo.png" alt="Hippo" height="200" id=hippo>
<div id=hippos>
</div>
</center>
</body>
</head>

JS:

function bigImg(x) {
    x.style.height = "410px";  
}

function normalImg(x) {
    x.style.height = "200px";
}
function handleClickPar() {
     document.getElementById("hippos").innerHTML = "Picture of a Hippo." ;
}

最佳答案

您缺少 onclick 来使 handleClickPar() 执行任何操作。

在该函数中,检查 hippo 是否为空 - 如果是,则设置文本。如果不是,请清除它。

function bigImg(x) {
  x.style.height = "410px";
}

function normalImg(x) {
  x.style.height = "200px";
}

function handleClickPar() {
  var el = document.getElementById("hippos");

  if (el.innerHTML.trim())   // if it's not empty or whitespace
    el.innerHTML = "";       
  else
    el.innerHTML = "Picture of a Hippo.";
}
<div id=hippos>
</div>

<img onmouseover="bigImg(this)" onmouseout="normalImg(this)" onclick="handleClickPar();" border="0" src="http://placehold.it/200" alt="Hippo" height="200" id=hippo>

关于javascript - 通过滚动使图像变大,单击 javascript 时显示文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30399130/

相关文章:

javascript - 使用 JavaScript 在 Google map 中叠加多个图像

javascript - node.js - 如何将多个图像发送到服务器上的页面

php - 在哪里实现连续访问计数器?

html - 让一堆 div 在向右浮动的 div 中正确排列

javascript - 按下后退按钮刷新页面

css - 当在 slider 缩略图上方时,子菜单会在悬停时消失

javascript - 在 JavaScript 中计算两个日期之间的百分比

javascript - 为什么即使我已经包含了引用资料,jquery marquee 也不起作用?

html - 使用 CSS 交换图像和文本(包括图片)

jquery - 悬停在 jQuery 增量 fontSize 错误