javascript - 单击鼠标,隐藏链接并显示

标签 javascript jquery html

当鼠标点击时,我需要隐藏链接并显示另一个 div。

<div class="fetch-from-link">
 <a href="#" class="test" onClick="$(this).parent().hide();">
  Fetch from link
</a>

<div class="hello">Hello world</div>
</div>

我只是使用简单的隐藏方法。但是如何在链接隐藏后显示我的“hello”div?

最佳答案

由于使用了 jQuery 来绑定(bind)事件处理程序,而不是丑陋的内联点击处理程序。

您需要hide() 当前元素,然后Class Selector ('.hello')可用于显示另一个 div。

jQuery(function($) {
  $('.test').click(function(e) {
    e.preventDefault();
    $(this).hide();
    $('.hello').show();
  })
});
.hello {
  display: none
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="fetch-from-link">
  <a href="#" class="test">Fetch from link</a>
  <div class="hello">Hello world</div>
</div>


根据当前的 HTML,您可以使用 .next()

Get the immediately following sibling of each element in the set of matched elements. If a selector is provided, it retrieves the next sibling only if it matches that selector.

jQuery(function($) {
   $('.test').click(function(e) {
      e.preventDefault();
       $(this).hide().next().show();
   })
});

关于javascript - 单击鼠标,隐藏链接并显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39955398/

相关文章:

javascript - 如何使用 JS 根据 SVG <text><textPath> 中的总字数查找 startOffset 并动态替换

php - 为什么我不能更改根目录?

javascript - 未捕获的类型错误 : Cannot read property 'barcodeScanner'

javascript - 用javascript显示html5音频的当前音量?

javascript - 将句子中的单词替换为格式化的 html

javascript - 如何在图像上创建蒙版(不是剪辑路径,而是相反)

javascript - Instagram 弹出窗口

javascript - 如何将图像像素的贝塞尔曲线生成为预定义形状

jquery - 发送 JSON 内容类型 + JavaScript 对象(jQuery ajax)

javascript - 防止 Angular Ng-Click 上的区域标签页面重新加载