【问题标题】:Why my toggle visibility javascript function is not working?为什么我的切换可见性 javascript 功能不起作用?
【发布时间】:2014-09-25 16:34:27
【问题描述】:

我有这个文件夹结构:

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";

然后,b.php:

<script type="text/javascript" src="js/common.js"></script>

然后,c.php:

<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 运行该站点。

问题是 gotourl 函数正在运行,但 togglevisibility 函数(在链接 show data 上)不起作用。如果我将函数的内容复制粘贴到内联 javascript 中(如display data 链接),它就可以工作。你能提示一下哪里出了问题吗?我已经搜索了几个小时的问题。一切似乎都是正确的。

【问题讨论】:

  • 你真的应该研究一下 jQuery。有了它,做这样的事情会容易得多。
  • 控制台里有东西吗?或者可能是开发工具网络选项卡上的 404 错误?
  • @BaileyHerbert 我应该有。但是由于在办公室的协作项目中,有时jQuery莫名其妙地不可用(不包括或包括但不能使用),我想我会尽量同时掌握基础以及jQuery,以防万一。 :)
  • @CamilleHodoul 不,什么都没有出现。什么也没有发生。我使用 Firebug。
  • 这个jsfiddle.net/JT93H 在点击显示时会产生ReferenceError: togglevisibility is not defined {"error": "Please use POST request"},但在点击显示时有效。

标签: javascript php function include


【解决方案1】:
<a href="e.com">E</a>

“href”是 URL,因此您应该将“window.location”替换为“window.location.href”。

请参阅文档:https://www.w3schools.com/js/js_window_location.asp

对于您的 togglevisibility(),我不确定。

【讨论】:

    猜你喜欢
    • 2014-04-18
    • 2016-10-06
    • 2013-09-13
    • 1970-01-01
    • 1970-01-01
    • 2010-11-21
    • 1970-01-01
    • 2011-06-23
    • 2020-12-17
    相关资源
    最近更新 更多