2010-04-07 3 views
13

परिदृश्य मेरे पास दो div एस है: एक वह जगह है जहां मैं आइटम (divResults) का चयन करता हूं और यह अगले div (divSelectedContacts) पर जाता है। जब मैं इसे चुनता हूं तो मैं इसके आगे एक टिक चिह्न डालता हूं। मैं क्या करना चाहता हूं जब मैं इसे फिर से चुनूं, तो मैं टिक मार्क को हटाना चाहता हूं और divSelectedContacts से तत्व को भी हटा सकता हूं।

$("#divResults li").click(function() 
{ 
    if ($(this).find('span').size() == 1) 
    { 
     var copyElement = $(this).children().clone(); 
     $(this).children().prepend("<span class='ui-icon ui-icon-check checked' style='float:left'></span>"); 
     $("#divSelectedContacts").append(copyElement); 
    } else 
    { 
     var deleteElement = $(this).find('span'); //here is the problem how to find the first span and delete it 
     $(deleteElement).remove(); 
     var copyElement = $(this).children().clone();//get the child element 
     $("#divSelectedContacts").find(copyElement).remove(); //remove that element by finding it 
    } 
}); 

मैं $(this) का उपयोग कर पहली span एक li में चयन करने के लिए पता नहीं कैसे:

यहाँ कोड है। किसी भी प्रकार की मदद की बेहद सराहना की जाती है।

$(this).find(":first-child"); 

उत्तर

26

कई तरीके:

$(this).find('span:first'); 

$(this).find(':first-child'); 

$(this).find('span').eq(0); 

ध्यान दें कि आप $(deleteElement)deleteElement के रूप में उपयोग करने के लिए की जरूरत नहीं है पहले से ही एक jQuery वस्तु है

+0

धन्यवाद इतना :-)। DeleteElement मेरे लिए नहीं हुआ था। अच्छी पकड़। – Raja

5

पहले बच्चे $(this) उपयोग इस के प्राप्त करने के लिए । तो अगर आप इसे इस तरह कर सकते हैं:

$(this).find('span:first').remove(); 
5

या तुम सिर्फ यह सब चयनकर्ता में फेंक सकता है ...

$('span:first', this); 
+0

मुझे यह पसंद है, अच्छा है! – Gabe

+0

मीठा मुझे इस कार्यान्वयन पसंद है। आपके लिए +1 – Raja

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