为什么我的切换可见性javascript函数不起作用?
我有以下文件夹结构:为什么我的切换可见性javascript函数不起作用?,javascript,php,function,include,Javascript,Php,Function,Include,我有以下文件夹结构: htdocs/ - a.php - plugin/ - b.php - website/ - c.php - js/ - common.js 我在common.js上有以下函数: function togglevisibility (id) { var doc = document.getElementById(id); if (doc.style.display == "" || doc.style.d
htdocs/
- a.php
- plugin/
- b.php
- website/
- c.php
- js/
- common.js
我在common.js
上有以下函数:
function togglevisibility (id)
{
var doc = document.getElementById(id);
if (doc.style.display == "" || doc.style.display == "block") doc.style.display = "none"; else
if (doc.style.display == "none") doc.style.display = "block";
}
function gotourl (url)
{
window.location = url;
}
现在,假设我在a.php
上有这个脚本:
include_once "plugin/b.php";
include_once "website/c.php";
<script type="text/javascript" src="js/common.js"></script>
<a href="#" onclick="togglevisibility('data');">show data</a><br>
<a href="#" onclick="
var doc = document.getElementById('data');
if (doc.style.display == '' || doc.style.display == 'block') doc.style.display = 'none'; else
if (doc.style.display == 'none') doc.style.display = 'block';">
display data</a>
<div id="data" style="display:none">This is the data</div>
<a href="#" onclick="gotourl('www.google.com');">goto google.com</a>
然后,b.php
:
include_once "plugin/b.php";
include_once "website/c.php";
<script type="text/javascript" src="js/common.js"></script>
<a href="#" onclick="togglevisibility('data');">show data</a><br>
<a href="#" onclick="
var doc = document.getElementById('data');
if (doc.style.display == '' || doc.style.display == 'block') doc.style.display = 'none'; else
if (doc.style.display == 'none') doc.style.display = 'block';">
display data</a>
<div id="data" style="display:none">This is the data</div>
<a href="#" onclick="gotourl('www.google.com');">goto google.com</a>
然后我们使用http://localhost/a.php
问题是,gotour
函数正在工作,但是togglevisibility
函数(在链接显示数据
上)不工作。如果我将函数的内容复制粘贴到内联javascript中(如显示数据
链接),它就工作了。你能告诉我哪里出了问题吗?我已经搜索这个问题好几个小时了。一切似乎都是正确的。
<a href="e.com">E</a>
“href”是URL,因此您应该将“window.location”替换为“window.location.href”
见文件:
至于您的togglevisibility(),我不确定。您应该真正研究jQuery。有了它,做这样的事情会容易得多。你的控制台里有什么东西吗?或者在“开发工具网络”选项卡上出现404个错误?@BaileyHerbert我应该有。但有时候jQuery在办公室的协作项目中莫名其妙地不可用(未包含或包含但不能使用),我想我会尝试同时掌握基本的jQuery和jQuery,以防万一。:)@卡米莱霍杜尔:不,什么都没发生。什么也没发生。我使用Firebug。这会产生一个
ReferenceError:togglevisibility未定义{“错误”:“请使用POST请求”}
单击show,但在单击display时起作用。