jQuery - page refreshing after click

时间:2016-11-26 18:34:43

标签: jquery

I wrote the following jQuery method, to do something when i click the button with the id 'add':

$(function () {
   $('#add').on('click', function () {
     $('#headline').text("Hurra!");
   });
})

When i add a breakpoint in chromium, i can see that it enters the method, changes the content of the headline, and then changes it back to its original state for some reason (that happens while the debugger is in the minified jquery script, so i don't really know what's happening). Can anyone give me a hint on what might be going on?

1 个答案:

答案 0 :(得分:1)

页面正在刷新,因为表单已提交。

如果您不想提交,请使用event.preventDefault();$(function () { $('#add').on('click', function(event) { $('#headline').text("Hurra!"); event.preventDefault(); }); })

//test inheritance
//parent class
var mom = function(name){
    this.momsname = name;
}
mom.prototype.getMomsName = function(){ return this.momsname; }

//child class
var kid = function(mother,name){
    this.sonsname = name;
    //call the parent
    mom.call(this,mother);
}
//add the parent's methods
kid.prototype = new mom();
//point the constructor to the child
kid.prototype.constructor = kid;
kid.prototype.getKidsName = function() { return this.sonsname }
//instatiate the child
var son = new kid("Mary","Tom");
//use only the child's methods
var temp2 = "Son's name: " + son.getKidsName() + ". Mother's Name: " + son.getMomsName() + ".";
//temp now holds "Son's name: Tom. Mother's Name: Mary."