简体   繁体   English

在布局上链接Javascript函数,该布局是从Rails中的ruby资产管道加载的?

[英]Linking a Javascript function on a layout that is loaded from the asset pipeline in ruby on rails?

When I link a function from an asset pipeline loaded javascript file(app/assets/javacripts/bootstrap.js) in my static page layout upon inspection I get: Uncaught ReferenceError: myFunction is not defined. 当检查时从静态页面布局中的资产管道加载的javascript文件(app / assets / javacripts / bootstrap.js)链接函数时,我得到:未捕获的ReferenceError:未定义myFunction。 I have googled for hours and without using Coffeescript or Jquery or methods inside of ruby I would simply like to link this function from my .js file into an layout in my views. 我已经搜索了几个小时,并且没有使用ruby内部的Coffeescript或Jquery或方法,我只是想将此函数从我的.js文件链接到视图中的布局中。 Any specific explanation or if you could link me to any resources on how to do this...or something that is a tutorial designed to quickly link Javascript into views not disturbing the pipeline that would be great. 任何具体的解释,或者您是否可以将我链接到有关如何执行此操作的任何资源...,或者是旨在将Javascript快速链接到视图而不会打扰管道的教程,这些都是很好的。 Thank you in advance. 先感谢您。

Here is the Javascript function: 这是Javascript函数:

  /* When the user clicks on the button, 
  toggle between hiding and showing dropdown content */
  function myFunction() {
    document.getElementById("myDropdown").classList.toggle("show");
  }

  // Close the dropdown menu if the user clicks outside of it
  window.onclick = function(event) {
    if (!event.target.matches('.dropbtn')) {

      var dropdowns = document.getElementsByClassName("dropdown-content");
      var i;
      for (i=0; i < dropdowns.length; i++) {
        var openDropdown = dropdowns[i];
        if (openDropdown.classList.contains('show')) {
          openDropdown.classList.remove('show');
        }
      }
    }
  }

Here is the corresponding css: 这是对应的CSS:

/* Dropdown Button */
.dropbtn {
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

/* Dropdown button on hover & focus */
.dropbtn:hover, .dropbtn:focus {
  background-color: #3e8e41;
}

/* The container <div> - needed to position the dropdown content */
.dropdown {
  position: relative;
  display: inline-block;
}

/* Dropdown content (Hidden by Default) */
.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

/* Links inside the dropdown */
.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #f1f1f1}

/* Show the dropdown menu (use JS to add this class to the .dropdown-    content container when the user clicks on the dropdown button) */
.show {display:block;}

Here is my html: 这是我的html:

                <li>
                    <div class="dropdown">
                        <button onclick="myFunction()" class="dropbtn">Dropdown</button>
                        <div id="myDropdown" class="dropdown-content">
                            <a href="#work_places">Work Places</a>
                            <a href="#living_places">Living Places</a>
                            <a href="#learning_places">Learning Places</a>
                            <a href="#interiors">Interiors</a>
                            <a href="#miscellaneous">Miscellaneous</a>
                        </div>
                    </div>
                </li>

Try defining your function this way to (hopefully) make it global and accessible in your view: 尝试以这种方式定义函数,以(希望)使其在您的视图中全局且可访问:

this.myFunction = function() {
  document.getElementById('myDropdown').classList.toggle('show');
};

Otherwise you can remove onclick from the view and attach it within the javascript file: 否则,您可以从视图中删除onclick并将其附加到javascript文件中:

document.getElementsByClassName('dropbtn')[0].onclick = function() {
  document.getElementById('myDropdown').classList.toggle('show');
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM