javascript - 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 会自动加载所有内容?谢谢!
解决方案
推荐阅读
- javascript - 如何让 Ionic 3 应用程序从 Ionic 1 应用程序使用的 window.localstorage 读取现有数据?
- react-native - 无法解析模块“react-redux”...模块“react-redux”在加速模块映射中不存在
- c++ - 如何正确格式化此打印功能?
- java - Json 没有被写入文件
- javascript - 在 JS 中获取用户定义的 CSS 值
- azure-active-directory - dataLocationCode 字段在多地理位置的图形 API 中不可用
- keycloak - 根据有效角色获取 keycloak 用户
- javascript - javascript/typescript/angular 中的逗号分隔符
- spring-boot - CCAvenue 与 Angular 7 + spring boot + mongo 集成
- javascript - 更改材质ui文本字段的边框底部颜色