简体   繁体   English

jQuery Toggle在网址后添加#,e.preventDefault(); 不工作

[英]JQuery Toggle adding # after URL, e.preventDefault(); not working

Having an issue where on IE, a # is being added to the end of the URL, after clicking on a toggle. 在IE上出现问题时,单击切换后,在URL的末尾添加了#。 (ie website.com/direc/main.html* # * (即website.com/direc/main.html*#*

This is disabling all the toggle functions. 这将禁用所有切换功能。 Haven't been able to duplicate the issue on FireFox. 尚未能够在FireFox上重复该问题。 I have an e.preventDefault(); 我有一个e.preventDefault(); in each function but I have not been able to resolve this issue by putting e.prevent in different locations. 在每个函数中,但我无法通过将e.prevent放在不同位置来解决此问题。

Code Below: 下面的代码:

    $(document).ready(function(){
      $("#1_EH").click(function(e){ 
        $("#1_S").slideToggle("slow");
e.preventDefault(); 
        }); 
    });
    $(document).ready(function(){
      $("#2_EH").click(function(e){ 
        $("#2_S").slideToggle("slow"); 
        e.preventDefault(); 
        }); 
     });

<a href="#" id="1_EH"><img src="pic.jpg" border="0" alt="" /></a>
<div class="client_box" id="1_S">
    <h2>text</h2>
    <p>text<a href="http://www.url.com" target="_blank">Directions &raquo;</a></p>
</div>

IDs can't start with digits; ID不能以数字开头; the result is undefined and (presumably) broken in Internet Explorer. 结果是不确定的,并且(大概)在Internet Explorer中损坏了。 Fix your ID attributes ( 1_EH / 1_S /etc) to start with a letter instead of a digit. 将ID属性( 1_EH / 1_S / etc)固定为以字母开头而不是数字开头。

e.preventDefault() should be the first line following function(e): e.preventDefault()应该是函数(e)之后的第一行:

   $("#2_EH").click(function(e){ 
            e.preventDefault(); 
    ...stuff to be done...
    });

Example code fixed here: http://jsbin.com/welcome/54005/ 此处固定的示例代码: http : //jsbin.com/welcome/54005/

Not removing, however, please note that from the poster of the correct answer, this answer is Factually incorrect and should be disregarded. 但是,请注意,从正确答案的发布者中,该答案实际上是错误的,应忽略不计。

尝试从链接中的href移除哈希标签。

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

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