简体   繁体   English

锚标记中的功能不起作用

[英]Function in Anchor tag doesn't work

So I have a couple functions: 所以我有几个功能:

function Toggle(id)
{
   EToggle(document.getElementById(id));
}

function EToggle(element)
{
   var subject = element.getElementsByTagName("div")[0];
   if(subject.style.display == "none")
   {
      subject.style.display = "block";
   }
   else
   {
      subject.style.display = "none";
   }
}

The Toggle function is called upon load, and works fine. Toggle函数在加载时被调用,并且工作正常。 However, when I say 但是,当我说
<a href="javascript:Toggle(flat)"> , nothing happens. <a href="javascript:Toggle(flat)"> ,什么都没有发生。

So the question is, why doesn't it work in the link, but on the page load? 所以问题是,为什么它不能在链接中起作用,但是在页面加载时起作用?

If you need more details, here is my website. 如果您需要更多详细信息,请访问我的网站。

use 采用

<a href="javascript:Toggle('flat')">

instead of 代替

<a href="javascript:Toggle(flat)">

Firstly, use single quotes around the id-strings in the html, as Matei mentioned. 首先,如Matei所述,在html中的id字符串周围使用单引号。

Next, change line 31 of collapsible.js 接下来,更改collapsible.js的第31行

You're asking for the first div contained by the clicked element. 您要求的是clicked元素所包含的第一个div。 The problem is, your <a> tag itself doesn't have any. 问题是您的<a>标记本身没有任何标记。 You're after the div that follows the <a> So, you want to get the parent node's div elements. 你以后跟随 <A>股利所以,你想要得到的父节点的div元素。

var subject = element.parentNode.getElementsByTagName("div")[0];

Remember, your structure is like this: 记住,您的结构是这样的:

<div class="collapsible" id="demo">
    <a href="javascript:Toggle('demo')"><h1>Demolition and Removal</h1></a>
    <div style="display: none; ">
        <p>Content removed from here</p>
    </div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM