我想调用一个按钮点击事件的函数。该功能是在另一个js文件中定义的,我在function.php
文件中有enqueue
该文件。我可以看到该文件已从enqueue
加载。但我得到错误按钮点击调用函数不起作用
errorReferenceError: myFunction is not defined
当我点击那个按钮。我要显示该按钮的下拉菜单中单击
<button onclick="jQuery(document).ready(function($) {myFunction();});" class="dropbtn"><i class="fa fa-bars fa-lg" aria-hidden="true"></i></button>
custom.js
(function($) {
function myFunction() {
document.getElementById('myDropdown').classList.toggle('show');
}
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');
}
}
}
}
})(jQuery);
只需从外部写'myFunction'到'$(function(){})'。并且你的定位属性应该是'onclick = return myFunction();' – Vineet