プロジェクトの要素に一度クリック イベントを登録しようとしています。
コードを単純化しましたが、基本的に私の構造は次のようになります。
function objectParent(){
}
objectParent.prototype.click=function()(
$('#create').click(function(){
var var1=new objectchild();
var1.add();
})
}
function objectchild(){
}
objectchild.prototype.add=function()(
//The '#aButton' click event will be registered 4 times if the '#create' button is clicked 4 times.
//so the alert will pop 4 times when 'aButton' is clicked which is not what I wanted.
//I can use $('#aButton').unbind('click') to remove the click event but I have many buttons in my project
//and I want to have better solution for this.
$('#aButton').click(function(){
alert('click')
})
}
var test=new objectParent()
test.click();
html
<a id='create' href='#'>test</a>
//everytime this <a> is clicked, it will create a new objectchild object
//after click 4 times of this button and click 'aButton', it will pop the alert box 4 times
//I know it register click event 4 times to the 'aButton'
//I only want to pop the alert box once.
//I can use $('#aButton').unbind('click); to remove the click event but I have many many buttons in my codes
//ex bButton, cButton...etc. I want to have a better approach for my codes.
<a id='aButton' href='#'>button</a>
私の問題をうまく説明できれば幸いです。助けてくれてありがとう!