开发者

Append to a div using jquery

开发者 https://www.devze.com 2022-12-13 05:58 出处:网络
I\'m trying to add content to a div every time a link 开发者_开发知识库is clicked. However, the added text dissapears immediately after being added. What am I doing wrong?

I'm trying to add content to a div every time a link 开发者_开发知识库is clicked. However, the added text dissapears immediately after being added. What am I doing wrong?

 <html>                                                                  
 <head>                                                                  
 <script type="text/javascript" src="/files/jquery.js"></script>          
 <script type="text/javascript">                                         
     $(document).ready(function() {
      $("a").click(function() {
      $("#questions").prepend("A question<br />"); 
      });   
});

</script>                                                               
</head>                                                                 
<body>                                                                  
 <a href="">Add</a>
   <p id = "questions"> </div>
   </body>                                                                 
   </html>


Add return false; to your click-event, to prevent it from reloading the page.


<a href="#">Add</a>

You need to have something in your "href", else it'll go navigate to the same page (at least for Firefox).


I agree with O.K.W

also putting return false; or passing through a variable to represent the control on the function and performing preventDefault will stop the natural click event.

function(e){
e.preventDefault();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消