Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript阻止默认不工作_Javascript_Jquery_Preventdefault - Fatal编程技术网

Javascript阻止默认不工作

Javascript阻止默认不工作,javascript,jquery,preventdefault,Javascript,Jquery,Preventdefault,我知道那里有很多关于preventDefault()不起作用的帖子,但我去了那些帖子,却一无所获 我所做的只是捕获一个单击事件,然后从响应中附加html,但是preventDefault()并没有阻止页面重新加载,我不知道为什么 这是我的HTML: <div class="slide_pane"> <div class="profile_content"></div> <div class="account_bottom_bar"> &

我知道那里有很多关于preventDefault()不起作用的帖子,但我去了那些帖子,却一无所获

我所做的只是捕获一个单击事件,然后从响应中附加html,但是preventDefault()并没有阻止页面重新加载,我不知道为什么

这是我的HTML:

<div class="slide_pane"> 
 <div class="profile_content"></div>

 <div class="account_bottom_bar">
  <ul class="bottom_bar_list">
    <a href="/profiles/load" class="sidebar_link">
      <li>
        <p>Content</p>
      </li>
    </a>
  </ul>
 </div>
但是当我点击这个动作时,它只是从一个新的屏幕中的响应加载HTML


有什么想法吗??谢谢

由于HTML中没有侧边栏链接,所以什么也不会发生

event.preventDefault()的使用示例:


下面是一个如何在链接上使用event.preventDefault()的示例:

示例中的
.sidebar\u link
元素在哪里?看起来每当用户滚动页面时,您都在向该元素添加一个新的事件处理程序,这真的很糟糕。可能是抱歉的Felix Kling的重复。刚刚编辑过。。。它在linkOne上,一个潜在的问题可能是您的HTML无效。只有
li
元素是
ul
元素的有效子元素。在
li
元素内移动链接。但除此之外,您的代码似乎正常工作,至少在Firfox中是这样:。我正在使用您的代码,但它不会在此处重新加载:我是否遗漏了什么?所以我注意到我使用的是document.scroll而不是.ready:P。但奇怪的是,当我启动chrome inspector并点击该操作时,它不会重新加载,但当我没有让检查员启动时,它会重新加载。。。我真的不明白part@user3029619:可能是打开检查器触发了滚动事件。很公平-我对onScroll事件表示怀疑:))
$(document).scroll(function() {
 ajaxView();
});

var ajaxView = function() {
 $('.sidebar_link').click(function(e) {
 e.preventDefault();
 $.ajax ({
  type: 'GET',
  url: $(this).attr("href")

  }).done(function(resp) {
   $('.profile_content').append(resp);
  });
 });
};
$(document).ready(function() {
    $(".sidebar_link").click(function(event) {
        event.preventDefault();
        $(this).css("color", "red");
    });   
});