javascript - 在 $(document).ready 上应用函数 onclick 函数

标签 javascript html css

我有 3 个 div,它们在页面加载时按顺序淡出,就像在这个链接上一样: http://jsfiddle.net/x4qjscgv/6/但是,我希望在用户单击按钮后发生淡入淡出功能。我尝试使用函数:document.getElementById,但它似乎不起作用。

完整代码如下:

<html>

<head>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>

$(document).ready(function() {
  $('.word1, .word2, .word3').each(function(fadeIn) {
    $(this).delay(fadeIn * 500).fadeIn(1000);
  });
});

document.getElementById('btn').onclick = function(e)

</script>

<style>
    #chat {
      display: none;
    }
</style>

</head>

<body>

<button id="btn"> fade divs </button>

<div id="chat" class="word1">Word 1</div>
<div id="chat" class="word2">Word 2</div>
<div id="chat" class="word3">Word 3</div>


<div id="" class="">Word 4</div>

</body>

</html>

最佳答案

你可以效仿我的例子。但请注意,id 只是一个实例,而 class 可以有多个实例!

  $(document).ready(function() {
    $('.word1, .word2, .word3').each(function(fadeIn) {
      $(this).delay(fadeIn * 500).fadeIn(1000);
    });
  });
  
  $("#btn").on('click', function(e){
    $('.word1, .word2, .word3').each(function(fadeIn) {
      $(this).delay(fadeIn * 500).fadeIn(1000);
    });
  });
  $("#btnhide").on('click', function(e){
    $('.chat').hide();
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<head>
<style>
  .chat {
    display: none;
  }
</style>
</head>
<body>

  <button id="btn"> fade divs </button> <button id="btnhide"> hide divs</button>
  
  <div class="chat word1">Word 1</div>
  <div class="chat word2">Word 2</div>
  <div class="chat word3">Word 3</div>
  
  
  <div id="" class="">Word 4</div>
</body>

关于javascript - 在 $(document).ready 上应用函数 onclick 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52528695/

相关文章:

javascript - 触发 ng-show 仅适用于 ng-click 事件

javascript - 动态删除工具提示中的值时出现问题

javascript - Angular 在外部 Controller 中使用带有对象的 jQuery 插件

html - 响应式导航不工作,丢失了我的 CSS 概览

javascript - 动画 scrollTop 不工作

javascript - 调用类似 : <function> <statement> 的函数

php - 如何在彼此相邻的两个单选按钮之间添加间距

javascript - jQuery 自定义滚动条不起作用

javascript - 拆分字母并分配类别

html - 第 n 个 : child/of-type does not seem to work on my site