简体   繁体   English

HTML标签<a>href和onclick</a>

[英]HTML tag <a> Both href and onclick

I'm working on this project and I'm really stuck on 1 thing: I would like to make a submenu with all sub-pages in one html file. 我正在从事这个项目,我真的只停留在一件事情上:我想在一个html文件中创建一个包含所有子页面的子菜单。

Whenever I use both href and onclick it does not work. 每当我同时使用hrefonclick它均无效。

It only works when I change the href into "#", but that would make the page inaccessible from other pages. 仅当我将href更改为“#”时,它才有效,但这会使该页面无法从其他页面访问。

Please note that changing the return value of the function does not help. 请注意,更改函数的返回值无济于事。

Thanks so much in advance! 非常感谢!

Here is my HTML: 这是我的HTML:

<script>
function show(shown, hidden, hidden2) {
  document.getElementById(shown).style.display='block';
  document.getElementById(hidden).style.display='none';
  document.getElementById(hidden2).style.display='none';
  return true;
}
</script>

<ul>                
<li><a href="../info/index.html" onclick="return show('Page1','Page2','ṕage3');">Doelstelling</a></li>
<li><a href="../info/index.html" onclick="return show('Page2','Page1','ṕage3');">Bestuur</a></li>
<li><a href="../info/index.html" onclick="return show('Page3','Page1','ṕage2');">Lid worden</a></li>
</ul>

<div id="Page1" style="display:none">
<a href="#" onclick="return show('Page2','Page1','ṕage3');">Bestuur</a></br>
<a href="#" onclick="return show('Page3','Page1','ṕage2');">Lid worden</a>   </br>
<p>

text

</p>

</div></br>

<div id="Page2" style="display:none">
<a href="#" onclick="return show('Page1','Page2','ṕage3');">Doelstelling</a>    </br>
<a href="#" onclick="return show('Page3','Page1','ṕage2');">Lid worden</a></br>
<p>

text

</div></br>
  <div id="Page3" style="display:none">
<a href="#" onclick="return show('Page1','Page2','ṕage3');">Doelstelling</a>  </br>
<a href="#" onclick="return show('Page2','Page1','ṕage3');">Bestuur</a></br>
    <p>
text
 </p>

</div></br>

Thanks again. 再次感谢。

return false; doesn't help you because an error triggers before this line of code. 对您没有帮助,因为在此行代码之前触发了错误。

Take a look at your show function call: you pass 'ṕage3' as a third argument, and then you do document.getElementById(hidden2).style.display = ... . 看一下show函数调用:将'ṕage3'作为第三个参数传递,然后执行document.getElementById(hidden2).style.display = ... But you don't have an element with id="ṕage3". 但是您没有id =“ṕage3”的元素。

Replace 'ṕage3' with 'Page3' , and this should do the trick 更换'ṕage3''Page3' ,这应该做的伎俩

You need to return false in your OnClick. 您需要在OnClick中返回false。 Since you are calling show in your onclick, return false instead of true. 由于您是在onclick中调用show ,因此返回false而不是true。

<script>
function show(shown, hidden, hidden2) {
  document.getElementById(shown).style.display='block';
  document.getElementById(hidden).style.display='none';
  document.getElementById(hidden2).style.display='none';
  return false;
}
</script>

When the user clicks on the anchor (the tag) it will first execute the onclick. 当用户单击锚点(标记)时,它将首先执行onclick。 If the onclick returns false it will stop. 如果onclick返回false,它将停止。 If the onclick returns true, then it will continue with the default behavior and change the window location to the value of the href attribute. 如果onclick返回true,则它将继续默认行为,并将窗口位置更改为href属性的值。

您需要使用event.preventDefault()禁用JavaScript中的默认事件处理;

 $("a").click(function() { alert(1); return false; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="http://google.com">Text</a> 

Don't use onclick in 2015. 在2015年不要使用onclick

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

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