2012-09-25 61 views
0

我有這樣的情況 -

$('<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)參考?

+3

爲什麼你不想在輸入第二個回調之前保存'$(this)'? –

+0

@ChristopherArmstrong這是我不想做的。我在評論中提到了相同的內容。 – Sriram

+2

您已經確定了唯一的(明智的)解決方案。系統變量'this'會被自動替換以反映當前範圍,因此要從不同範圍內引用它,您必須手動將其緩存在其他變量中。 –

回答

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

因此,在這種情況下,我們無法獲得對「P」對象的引用。對? – Sriram

+1

只有一個'this'關鍵字。你可以保持默認行爲並使其指向'p's,或者調整它並指向'div's,但是你不能這樣做。保持對不同對象的兩個引用的唯一方法是有兩個變量... – LeGEC

+0

我正在瀏覽http://api.jquery.com/jQuery.proxy/ API,但無法使用它。你能簡單地解釋這個方法的意義嗎? – Sriram

0

所以你想在第二個函數中使用第一個函數中的$(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

這個答案已經提供兩次然後刪除,因爲OP不希望它出於某種未知的原因。 –

+0

這是要走的路。我總是喜歡給任何jQuery對象變量添加一個'$'前綴來區分它們。 'var $ first = $(this);'但這是個人偏好。 – Shikyo

0

雖然這不是最好的解決方案(因爲你應該引用它通過像其他解決方案的變量建議)......你總是可以使用父()方法如果第二選擇器是第一個的孩子。

$('<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(); 
     }); 
    }); 
}); 
+1

我不認爲* firstselector *是* secondselector的父母* – Bergi

+0

您可能是對的......但我會留下答案,以防萬一。 –

+0

@ComputerArts #Bergi是對的。它不是第二選擇器的父代。 – Sriram

0

您可以在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

這不行! – Sriram

0

「解決」你的問題的唯一方法是不使用jQuery。

然而,你用jQuery標記你的問題。

你究竟想在這裏做什麼,爲什麼被你解僱的簡單(和一個常見的使用閉包的JavaScript習慣用法)?

+0

如果沒有jQuery,你將無法做到這一點 – Bergi

+0

@Bergi - 當然,你可以創建一個處理程序來創建你想要的上下文 - 就像jQuery一樣。你可以讓它成爲處理程序中的父項,沒有任何事會阻止你。 – Hogan

+0

不,原生'addEventListener'也使用DOM元素作爲上下文 - 與jQuery沒有什麼不同。使用'.bind()'創建自定義上下文也可以在jQuery中工作,就像OP不需要的解引用變量 – Bergi

0

在這種情況下對DOM準備好所有的第一選擇是綁定click事件處理程序。在點擊第一選擇器時,第二選擇器綁定到點擊事件。要使用$(this)哪個reperesent首選器,您必須編寫代碼爲

$('<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()); 
      }); 
     }); 
    }); 
}); 

首先點擊輸入文本字段。當點擊帶有hello類的按鈕時,它會提示輸入TEXT字段的值。

相關問題