私はjscriptオブジェクトを持っています。div にいくつかの html を挿入し、jquery クリック イベント関数内のオブジェクトのメソッドとプロパティにどのようにアクセスできるかによって、イベント クリックを html ラベルにアタッチしようとします。
HTML コード:
<div id="content"></div>
JAVASCRIPT コード:
<SCRIPT ...
jQuery().ready(function (){
obj=new myobject("myobject1","#content"); //create object instance
obj.dohtml(); //call method dohtml
});
//object I pass an objectname and a id from an html element
function myobject(objectname,htmlid){
this.name=objectname; //name of object
this.htmlid=htmlid; //id where i try to create some html
//say hello
this.hello=function(){
alert(hello);
}
//do something with html
this.dohtml=function(){
//create an <p> element in a the html with id=htmlid
$(this.htmlid).html('<p id="'+this.name+'_myp" > my text </p>')
//click event on <p> id= this.name+'_myp"
$("#"+this.name+'_myp').click(function(){
alert(this.name); //i try to access property this.name in the object and dont work
//how can access to this property from here
this.hello(); //how can access to this method from here
});
$("#"+this.name+'_myp').click(this.hello()); //this dont work too why?
}
}