/ / मैं किसी अन्य फ़ंक्शन के अंदर $(this) संदर्भ का उपयोग कैसे कर सकता हूं? - जावास्क्रिप्ट, jQuery, jquery-चयनकर्ता, यह

मैं किसी अन्य फ़ंक्शन के अंदर $ (यह) संदर्भ का उपयोग कैसे कर सकता हूं? - जावास्क्रिप्ट, jquery, jquery-selectors, यह

मेरे पास ऐसा मामला है -

$("<first-selector>").each(function(){
$(this).click(function(){
$("<second-selector>").click(function(){
/* Here I want to use the top $(this) reference to do some business.
I don"t want to use like var thisObj = $(this) and refer it inside
the click function*/
});
});
});

कैसे इस्तेमाल करे $(this) किसी अन्य वस्तु के अंदर संदर्भ?

उत्तर:

उत्तर № 1 के लिए 4

उपयोग $ .proxy

$("div").each(function(){
$(this).click(function(){
$("p").click($.proxy(function(){
console.log($(this)); // returns <div>
/* Here I want to use the top $(this) reference to do some business.
I don"t want to use like var thisObj = $(this) and refer it inside
the click function*/
}, this));
});
});

डेमो: http://jsfiddle.net/TRw4X/


जवाब के लिए 0 № 2

तो आप पहले से $(this) का उपयोग करना चाहते हैंदूसरे फ़ंक्शन के भीतर कार्य करें और इसे $(this)? यह संभव नहीं है क्योंकि jQuery इस संदर्भ को बनाए रखता है। आपको इस तरह से कुछ चलाना होगा:

$("<first-selector>").each(function()
{
var first = $(this);

first.click(function()
{
$("<second-selector>").click(function()
{
var second = $(this);
// do something with first and second.
});
});
});

जवाब के लिए 0 № 3

आप jQuery ईवेंट में ऑब्जेक्ट का संदर्भ पास कर सकते हैं।

$("<first-selector>").each(function(){
$(this).click($(this),function(){
$("<second-selector>").click(function(e){
var $elem = e.data.
/* Here I want to use the top $(this) reference to do some business.
I don"t want to use like var thisObj = $(this) and refer it inside
the click function*/
});
});
});

जवाब के लिए 0 № 4

अपने प्रश्न को "हल" करने का एकमात्र तरीका jQuery का उपयोग नहीं करना है।

फिर भी आप अपने प्रश्न को jQuery के साथ टैग करते हैं।

आप यहां वास्तव में क्या करना चाहते हैं और आपके द्वारा खारिज कर दिया गया आसान (और बंद करने का एक सामान्य जावास्क्रिप्ट मुहावरा) क्यों है?


जवाब के लिए 0 № 5

हालांकि यह सबसे अच्छा समाधान नहीं है (क्योंकिआपको अन्य समाधानों की तरह एक चर के माध्यम से इसका संदर्भ देना चाहिए) ... यदि दूसरा चयनकर्ता पहले का बच्चा है तो आप हमेशा माता-पिता() विधि का उपयोग कर सकते हैं।

$("<first-selector>").each(function(){
$(this).click(function(){
$("<second-selector>").click(function(){
/*$(this) is now referencing <second-selector> and by adding parent() it will go up the elements until it find the requested selector*/
$(this).parents("<first-selector>").doSomething();
});
});
});

जवाब के लिए 0 № 6

इस मामले में डोम पर सभी प्रथम-चयनकर्ता तैयार हैंक्लिक इवेंट हैंडलर से बंधे हैं। प्रथम-चयनकर्ता पर क्लिक करने पर, द्वितीय-चयनकर्ता क्लिक ईवेंट से जुड़ जाता है। $(this) का उपयोग करने के लिए जो पहले-चयनकर्ता को दर्शाता है, आपको कोड को इस प्रकार लिखना होगा

$("<first-selector>").each(function(){
$(this).click(function(){
var oldthis = $(this);
$("<second-selector>").click(function(){
alert(oldthis.val());
});
});

});

सुनिश्चित करें कि प्रथम-चयनकर्ता टैग दूसरे-चयनकर्ता टैग के समान नहीं है। इसे फॉर्म में आजमाएं

jQuery(document).ready(function(){
jQuery("input[type=text]").each(function(){
jQuery(this).click(function(){
var oldthis = jQuery(this);
jQuery("input.hello").click(function(){
alert(oldthis.val());
});
});
});
});

सबसे पहले इनपुट टेक्स्ट फील्ड पर क्लिक करें। जब हैलो क्लास वाला बटन क्लिक किया जाता है तो यह इनपुट टेक्स्ट फ़ील्ड के मूल्य को अलर्ट करेगा।