首页 > 解决方案 > wordpress 在特定页面上添加 javascript 函数得到未捕获的引用错误

问题描述

I have searched the net, maybe not the right place,s but have been trying to simply insert a dropdown list with a "go" button that when selected will take me to a page for that state. 这是我添加到主题功能页面的代码:

function wpb_hook_javascript() {
  if (is_page ('covid-19-resources-for-artists')) { 
    ?>
        <script type="text/javascript">
        // your javscript code goes here
          function gotosite() {
  window.location = document.getElementById("menu").value; // JQuery:  $("#menu").val();
}// your javscript code goes here
        </script>
    <?php
  }
}
add_action('wp_head', 'wpb_hook_javascript');

这是页面上的html

<select id="menu">
<option value="">Please Choose...</option>
<option value="http://www.google.com/">Google</option>
<option value="http://www.youtube.com/">Youtube</option>
<option value="http://www.pinterest.com/">Pinterest</option>
</select>
<button id="go" onclick="gotosite()">Go</button>

控制台错误是:?preview=true:324 Uncaught ReferenceError: gotosite is not defined at HTMLButtonElement.onclick (?preview=true:324)

谁能帮我解决我做错了什么?据说 wordpress 会自动加载所有内容?谢谢!

标签: javascriptwordpressmenu

解决方案


推荐阅读