我为事件监听器生成了一个构造函数。但这不是我想要的方式

时间:2019-07-25 08:22:24

标签: javascript onclick

我正在使用纯JavaScript创建视频切换页面。每当我单击“禁用控件”按钮时。该控件应被禁用。并单击“启用Btn”启用它。我听说了DRY原理。所以要完全遵循它。我为事件侦听器创建了一个构造函数,该函数应在向其传递“元素”,“事件”,“动作”参数后才能工作。

最初,我的代码中存在很多错误。通过,我即兴发挥了作用。 addEventListener附加到按钮上,它也为我提供了控制台日志。但是该动作并未执行。我不知道为什么请帮助。预先感谢。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Player</title>

    <style></style>

    <link
      rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css"
      integrity="sha256-YLGeXaapI0/5IgZopewRJcFXomhRMlYYjugPLSyNjTY="
      crossorigin="anonymous"
    />

  </head>
  <body>
    <h2 class="text-warning text-center">
      Video Player DOM Manipulation Using using Pure JS
    </h2>
    <p class="text-center">I have used Dry Principle so that code is compact</p>
    <div class="container">
      <div class="row">
        <div class="col-md-6 mx-auto">
          <video
            class="mx-auto"
            src="video\your_video.mp4"
            width="500px"
            autoplay="true"
            controls
          ></video>
        </div>
      </div>

      <div class="row mb-5">
        <div class="col-md-6 mx-auto">
          <p class="text-warning font-weight-bold">Controls :-</p>
          <button class="btn btn-sm btn-success enableBtn">Enable</button>
          <button class="btn btn-sm btn-success disableBtn">Disable</button>
        </div>
      </div>
    </div>

    <script>
      // window.addEventListener("DOMContentLoaded", function() {
        var fnConstruct = function(element, event, action) {
          console.log(element);
          console.log(event);
          // console.log(action);
          document
            .querySelector(element)
            .addEventListener(event, function() {
              console.log('click');
              console.log(action);
              action;
            });
        };

        var enableControls =
          'document.querySelector("video").removeAttribute("controls");document.querySelector("video").setAttribute("controls", "");';
        fnConstruct(".enableBtn", "click", enableControls);

        var disableControls =
          'document.querySelector("video").removeAttribute("controls");';
        fnConstruct(".disableBtn", "click", disableControls);


      // });
    </script>
  </body>
</html>

注意:-请将video src属性替换为您要在系统中使用的任何视频。

1 个答案:

答案 0 :(得分:1)

尝试将函数而不是字符串传递为action并在fnConstruct中调用它:

var fnConstruct = function(element, event, action) {
  console.log(element);
  console.log(event);
  document
    .querySelector(element)
    .addEventListener(event, function() {
      console.log('click');
      action();
    });
};

fnConstruct(".enableBtn", "click", function() {
  document.querySelector("video").removeAttribute("controls");
  document.querySelector("video").setAttribute("controls", "");
});

fnConstruct(".disableBtn", "click", function() {
  document.querySelector("video").removeAttribute("controls");
});