在RubyonRails中从资产管道加载的布局上链接Javascript函数?
在检查静态页面布局时,当我从资产管道加载的javascript文件(app/assets/javascripts/bootstrap.js)链接函数时,我得到:Uncaught ReferenceError:myFunction未定义。我在谷歌上搜索了几个小时,没有在ruby中使用Coffeescript、Jquery或方法,我只想将这个函数从.js文件链接到视图中的布局中。任何具体的解释,或者你是否可以将我链接到任何关于如何实现这一点的资源……或者是一个旨在快速将Javascript链接到视图而不干扰管道的教程,那将是非常棒的。先谢谢你 下面是Javascript函数:在RubyonRails中从资产管道加载的布局上链接Javascript函数?,javascript,ruby-on-rails,ruby,asset-pipeline,Javascript,Ruby On Rails,Ruby,Asset Pipeline,在检查静态页面布局时,当我从资产管道加载的javascript文件(app/assets/javascripts/bootstrap.js)链接函数时,我得到:Uncaught ReferenceError:myFunction未定义。我在谷歌上搜索了几个小时,没有在ruby中使用Coffeescript、Jquery或方法,我只想将这个函数从.js文件链接到视图中的布局中。任何具体的解释,或者你是否可以将我链接到任何关于如何实现这一点的资源……或者是一个旨在快速将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');
}
}
}
}
/*当用户单击按钮时,
在隐藏和显示下拉内容之间切换*/
函数myFunction(){
document.getElementById(“myDropdown”).classList.toggle(“show”);
}
//如果用户在下拉菜单外单击,请关闭下拉菜单
window.onclick=函数(事件){
如果(!event.target.matches('.dropbtn')){
var dropdowns=document.getElementsByClassName(“下拉内容”);
var i;
对于(i=0;i
以下是相应的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;}
/*下拉按钮*/
.dropbtn{
背景色:#4CAF50;
颜色:白色;
填充:16px;
字体大小:16px;
边界:无;
光标:指针;
}
/*悬停和聚焦上的下拉按钮*/
.dropbtn:悬停,.dropbtn:焦点{
背景色:#3e8e41;
}
/*容器-需要定位下拉内容*/
.下拉列表{
位置:相对位置;
显示:内联块;
}
/*下拉内容(默认情况下隐藏)*/
.下拉内容{
显示:无;
位置:绝对位置;
背景色:#f9f9f9;
最小宽度:160px;
盒影:0px 8px 16px 0px rgba(0,0,0,0.2);
}
/*下拉列表中的链接*/
.下拉内容a{
颜色:黑色;
填充:12px 16px;
文字装饰:无;
显示:块;
}
/*更改悬停时下拉链接的颜色*/
.下拉内容a:悬停{背景色:#f1f1}
/*显示下拉菜单(当用户单击下拉按钮时,使用JS将此类添加到.dropdown-content容器)*/
.show{display:block;}
这是我的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>
下拉列表
尝试以这种方式定义您的函数(希望)使其在您的视图中具有全局性和可访问性:
this.myFunction = function() {
document.getElementById('myDropdown').classList.toggle('show');
};
否则,您可以从视图中删除onclick并将其附加到javascript文件中:
document.getElementsByClassName('dropbtn')[0].onclick = function() {
document.getElementById('myDropdown').classList.toggle('show');
};
您是否尝试在浏览器中检查元素以检查javascript文件是否可用?在Chrome浏览器中,您可以在“源”选项卡中进行检查。另外,您的app/assets/javascripts/application.js文件包含哪些内容?