1

要素のユーザー関数 onclick を実行する必要がある jquery 関数を作成しました。問題は、関数を呼び出している要素にアクセスする必要があることです。これを達成するにはどうすればよいでしょうか。関数の例は次のとおりです。

                $("#domelement").yesNoDialog({
                 title:"Ineligible to Register",
                 message:"Stackoverflowtest",
                 yesFunction:function(){        
                     location.href = "www.stackoverflow.com"

                     //How do i grab an attribute from #domelement in here? ie $("domeElement").attr("stuff");

                 }
             });

Jquery メソッドの簡略版は次のとおりです。

(function($) {       
  $.fn.yesNoDialog = function(options) {     
    options = $.extend({}, $.fn.yesNoDialog.defaultOptions, options);      
    this.each(function() {
      $(this).click(function(){
          options.yesFunction();
       });
    });     
    return this;
  };
})(jQuery);
4

1 に答える 1

3

要素を関数に渡して、そのように使用できます

                $("#domelement").yesNoDialog({
                 title:"Ineligible to Register",
                 message:"Stackoverflowtest",
                 yesFunction:function(element){     
                     location.href = "www.stackoverflow.com"

                     //How do i grab an attribute from #domelement in here? ie $("domeElement").attr("stuff");

                 }
             });

要素を関数に渡す

(function($) {       
  $.fn.yesNoDialog = function(options) {     
    options = $.extend({}, $.fn.yesNoDialog.defaultOptions, options);      
    this.each(function() {
      var that = this;
      $(this).click(function(){
          options.yesFunction(that); <--- Lets you use the element 
       });
    });     
    return this;
  };
})(jQuery);
于 2013-01-23T19:19:52.870 に答える