2012-06-02 15 views
10

त्वरित सवाल लोगों टॉगल, मैं इस.on() और यहाँ एक साथ काम करने

$('#wrap').on('toggle', '#image', function(){ 
    <!-- Do stuff --> 
    }); 

इसके अंदर एक टॉगल करने के लिए सक्षम होने के लिए प्राप्त करने के लिए नहीं कर सकते लगता है? कोई विचार? मैंने googling कोशिश की है लेकिन कोई भाग्य के साथ।

इस

, कोड मैं .on के साथ काम करने की कोशिश कर रहा हूँ वर्तमान में यह तत्व पुत्र पेज, के सभी

$("#result_options").toggle(function() { 
     var image_width = $('#image').width()/2; 
     var brick_width = $('#brick').width()/2; 
     $("#image").css("width",image_width); 
     $("#image").css("padding","4px"); 
     $("#brick").css("width",brick_width); 
    },function(){ 
     $("#image").css("width","300"); 
     $("#image").css("padding","8px"); 
     $("#brick").css("width","314"); 
     $(this).html("Smaller Results"); 
    }); 
}); 
(#image और #brick है) में परिवर्तन लागू नहीं होता है के रूप में
+1

के संभावित डुप्लिकेट [ टॉगल ईवेंट के साथ jQuery .live का उपयोग करना] (http://stackoverflow.com/questions/2172614/using-jquery-live-with-toggle-event) – j08691

उत्तर

24

आपको जिस समस्या का सामना करना पड़ रहा है वह यह है कि toggle ईवेंट नहीं है; toggle() एक jQuery विधि है। एक toggle() को लागू करने के लिए, on() साथ मुझे लगता है कि आप परीक्षण करने के लिए है कि क्या कुछ चालू किए जाने पर किया गया है, या टॉगल बंद/नहीं-टॉगल

एक click घटना है, और फिर एक if बयान उपयोग करने की आवश्यकता होगी
$('#wrap').on('click', '#image', function(){ 
    if (!$(this).attr('data-toggled') || $(this).attr('data-toggled') == 'off'){ 
     /* currently it's not been toggled, or it's been toggled to the 'off' state, 
      so now toggle to the 'on' state: */ 
      $(this).attr('data-toggled','on'); 
      // and do something... 
    } 
    else if ($(this).attr('data-toggled') == 'on'){ 
     /* currently it has been toggled, and toggled to the 'on' state, 
      so now turn off: */ 
      $(this).attr('data-toggled','off'); 
      // and do, or undo, something... 
    } 
}); 
+0

@maryisdead: धन्यवाद! मुझे एहसास नहीं हुआ था (या मुझे लगता है कि इसे उपयोगकर्ता द्वारा परिभाषित कस्टम इवेंट के रूप में इस्तेमाल किया गया था)। =) –

+1

आपका स्वागत है! आपके उत्तर के लिए धन्यवाद! :-) – maryisdead

संबंधित मुद्दे