2016-08-21 39 views
0

我需要的$(this)內容的功能,我想知道這裏面

;(function($) { 
    $.fn.extend({ 
    myFunction : function() { 
    // if this contain "tr" i have to do some ... 
    } 
    }); 
    $("#myTable tbody tr").myFunction(); 
})(jQuery); 
+0

你是什麼意思的'內部的字符串?你的意思是標籤名稱? - 'this.tagName'。 – PeterKA

+0

你是指$(this)的內容是什麼意思?元素中包含的html是什麼? –

+0

我需要字符串「#myTable tbody tr」 – IfThenElse

回答

0

字符串您可以通過this.selector您的jQuery方法內訪問選擇像這樣:

;(function($) { 
 

 
    $.fn.extend({ 
 
     myFunction: function() { 
 
      console.log(this.selector); 
 
     } 
 
    }); 
 
    $("#myTable tbody tr").myFunction(); 
 

 
})(jQuery);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

然而,通常這將是優選的,以檢測是否您的元件是一個trtr的孩子使用this.is('tr')this.is('tr *')來代替,而不是試圖解析選擇器。

+0

我發現$(this).prop(「tagName」)並且正在工作,ty – IfThenElse