最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery click question - Stack Overflow

programmeradmin0浏览0评论

i have an issue.

the code is this:

$("#button").click(function(event){
   $("#threads").html("hi");
});

when i click the button, the text "hi" is just shown in 1 sec. then it disappear. i want it to always be shown after i have clicked. how can i do this?

i have an issue.

the code is this:

$("#button").click(function(event){
   $("#threads").html("hi");
});

when i click the button, the text "hi" is just shown in 1 sec. then it disappear. i want it to always be shown after i have clicked. how can i do this?

Share Improve this question asked Dec 6, 2009 at 21:33 never_had_a_namenever_had_a_name 93.2k105 gold badges277 silver badges392 bronze badges 3
  • What is the html for #button ? – PetersenDidIt Commented Dec 6, 2009 at 21:35
  • Do any other overlapping elements on the page have a click handler? For example, does the body of your document have a click handler? – Mark Byers Commented Dec 6, 2009 at 21:43
  • 2 @fayer You now have 21 questions inside of 3 days with no correct answers. Please take the time to go back and mark the correct answers before asking more questions. – Doug Neiner Commented Dec 6, 2009 at 22:15
Add a comment  | 

3 Answers 3

Reset to default 28

Try this:

$("#button").click(function(event){
   event.preventDefault();
   $("#threads").html("hi");
});

My guess is button is an [a href] tag or in a form which is causing the page to refresh.

The problem might be that your button is inside a form with no action, thus submitting the form back to the page - and reverting your change. Or that button is a link with an empty href.

Add return false; to the end of your click-function to stop the default action.

Your problem is that you have a timeout or interval that is running every 1000 milliseconds, clearing the HTML of #threads. Either it's running as a timeout (setTimeout) from another $("#button").click() or an interval (setInterval) defined somewhere else. I'm just guessing 1000 milliseconds because you said 1 second and I don't know if you mean for a moment instead.

发布评论

评论列表(0)

  1. 暂无评论