2016-08-21 78 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