e.preventDefault unstable并且不会阻止默认操作

时间:2013-01-26 05:51:21

标签: jquery events preventdefault

我试图让链接不做默认操作转到链接,而是当用户点击链接时它会执行剩下的代码,在这种情况下加载我用AJAX调用的相应url元素并将它们放在正确的Div / span中。这是代码......

 $(function(){
    $('body').append('<span id="view_mess">New Message</span><div id="mess_wrapper"><div id="new_mess_pop"><div id="button_wrap"><span id="post_date_time"></span><div id="users_contact_info"><div id="users_image"></div><div id="message_lister"></div></div><div id="message_post"></div><div id="inner_mess_wrapper"></div></div></div></div>');
      $('#message_lister').load('/privmsg?folder=inbox .tdtopics:lt(5)');
        $('#view_mess').click(function() {
      $('#mess_wrapper').show();
       });
 $('.topictitle').click(function(e) {
  var msg = $('.tdtopics a').attr('href');
   $.get(msg, function (data) {
     var elems = $(data);
      $('#post_date_time').append(elems.find('div.posthead h2'));
      $('#users_image').append(elems.find('.user-basic-info'));
      $('#message_post').append(elems.find('.entry-content'));
   }, 'html');
e.preventDefault();
  $('#mess_wrapper').show();
 });

我已经尝试了e.preventDefault();,它位于上面的代码中,紧跟在功能代码之后。也许我应该在第一个函数中写e?

2 个答案:

答案 0 :(得分:2)

Try with this :
Two things :you missed the end bracket of function  and e.preventdefault shuld be in start of the event 



 $(function(){
    $('body').append('<span id="view_mess">New Message</span><div id="mess_wrapper"><div id="new_mess_pop"><div id="button_wrap"><span id="post_date_time"></span><div id="users_contact_info"><div id="users_image"></div><div id="message_lister"></div></div><div id="message_post"></div><div id="inner_mess_wrapper"></div></div></div></div>');
      $('#message_lister').load('/privmsg?folder=inbox .tdtopics:lt(5)');
        $('#view_mess').click(function() {
      $('#mess_wrapper').show();
       });
 $('.topictitle').click(function(e) {
 e.preventDefault();
  var msg = $('.tdtopics a').attr('href');
   $.get(msg, function (data) {
     var elems = $(data);
      $('#post_date_time').append(elems.find('div.posthead h2'));
      $('#users_image').append(elems.find('.user-basic-info'));
      $('#message_post').append(elems.find('.entry-content'));
   }, 'html');
  $('#mess_wrapper').show();
 });


}

答案 1 :(得分:0)

尝试

return false;

这样可以防止默认,也可以防止传播