javascript - 单击按钮隐藏或取消隐藏文本

标签 javascript jquery html

我是 html 的新手,这是我们的第一个 javascript 入门作业;所以我很自然地吓坏了!这是我正在谈论的示例:

<div class="someclassname"> <a href="image.jpg">
           <img src= Image/image.jpg height="80">
    </a>
    <p>some text to he hidden with the image!</p>
</div>

我到处都找过类似的东西,但我无法将其转化为我正在做的事情

我在想代码应该看起来像这样吗?

<script>
$(document).ready(function() {
$("Button").click(function() {
$(".someclassname").toggle();
if ($.trim($(this).text()) == 'Hide') {
$(this).text('Show');
} else {
$(this).next('Hide');
}
});
</script>

我接近了吗?请帮忙!

最佳答案

你已经足够接近了......

您的脚本中存在语法错误,缺少一对 }) 然后在您的 html 中放置一个按钮

$(document).ready(function() {
  $("button").click(function() {
    $(".someclassname").toggle();
    if ($.trim($(this).text()) == 'Hide') {
      $(this).text('Show');
    } else {
      $(this).next('Hide');
    }
  });
}); //this is missing
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button>Hide</button>
<div class="someclassname">
  <a href="image.jpg">
    <img src="Image/image.jpg" height="80" />
  </a>
  <p>some text to he hidden with the image!</p>
</div>

关于javascript - 单击按钮隐藏或取消隐藏文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28868009/

相关文章:

php - 将 ../转换为真实目录 PHP

jQuery倒计时器,到0后自动刷新

javascript - Ajax回调不响应

javascript - 带有主干 Marionette 路由器的查询字符串

javascript - 尝试让 onclick 多次调用 Javascript

javascript - Adobe Analytics DTM - 阻止特定页面/子域上的初始服务器调用?

javascript - 发送前在DataTable上添加ajax数据

javascript - 半透明线不一致地显示为不透明

javascript 如何在任何事件上加载脚本或数据(如谷歌)而不使用ajax

html - 元素上的边距将整个页面向下移动