2008-09-18 14 views
33

मान लीजिए मेरे पास एक साधारण एक्सएचटीएमएल दस्तावेज़ है जो विशेषताओं के लिए कस्टम नेमस्पेस का उपयोग करता है:jQuery विशेषता चयनकर्ता: एक कस्टम नेमस्पेस के साथ एक विशेषता के लिए क्वेरी कैसे करें

<html xmlns="..." xmlns:custom="http://www.example.com/ns"> 
    ... 
    <div class="foo" custom:attr="bla"/> 
    ... 
</html> 

मैं प्रत्येक तत्व से कैसे मेल करूं जिसमें jQuery का उपयोग करके एक निश्चित कस्टम विशेषता है? Xzx21

$("div[custom:attr]") 

का उपयोग करना काम नहीं करता है। (अभी तक फ़ायरफ़ॉक्स के साथ कोशिश की।)

+0

अद्यतन, Suphi का जवाब एक बहुत सरल वाक्य रचना और काम करता है है। हालांकि मैंने कोई प्रदर्शन तुलना नहीं की है। –

+0

क्या नामस्थान उपसर्ग घोषणा xmlns होने चाहिए: custom =? – grantwparks

उत्तर

42

jQuery सीधे कस्टम नामस्थान का समर्थन नहीं करता, लेकिन आप फ़िल्टर फ़ंक्शन का उपयोग कर उन divs को ढूंढ सकते हैं जिन्हें आप ढूंढ रहे हैं।

// find all divs that have custom:attr 
$('div').filter(function() { return $(this).attr('custom:attr'); }).each(function() { 
    // matched a div with custom::attr 
    $(this).html('I was found.'); 
}); 
+0

मुझे ऐसा कुछ डर था। धन्यवाद! –

3

इधर देखो http://pastebin.me/48d233d998b4b

मूल रूप से अपने $ ('div') attr। ('कस्टम: attr')

+1

मैंने अपनी खोज में स्पष्ट किया: मैं प्रत्येक तत्व से मेल खाना चाहता हूं जिसमें कस्टम विशेषता है, विशेषता का मूल्य प्राप्त न करें। –

+0

@redsquare: यह अधिकांश ब्राउज़रों के लिए काम करता है लेकिन ओपेरा में विफल रहता है। इसके लिए कोई त्वरित फिक्स? – Gapipro

19

यह कुछ परिस्थितियों में काम करता है:

$("div[custom\\:attr]")

हालांकि, एक और अधिक उन्नत विधि के लिए, विशेषता के आधार पर मिलान के लिए this XML Namespace jQuery plug-in

+0

नामस्थान प्लग-इन एक सपना है। –

+0

यह रणनीति सफारी और क्रोम जैसे वेबकिट आधारित ब्राउज़र में काम नहीं कर रही है। कोई उपाय? – kaychaks

+1

+1, एक छोटी तरफ नोट के साथ: jQuery संलग्न एचटीएमएल भागों की नामस्थान परिभाषाओं को क्लॉबर करेगा (एसवीजी के साथ परीक्षण किया गया है) वास्तव में। यह गुणों को 'xmlns: custom = "uri" '' custom = "uri" में जोड़ता है, संभवतः क्योंकि HTML (आमतौर पर?)' Xmlns' विशेषता को स्वीकार नहीं करता है)। एक्सएचटीएमएल के रूप में दस्तावेज़ की सेवा समस्या को हल करता है, हालांकि सभी मामलों में व्यावहारिक नहीं हो सकता है। –

7

वाक्य रचना देखें:

$ ("div [customattr = bla]") मैचों --डिव customattr = "bla" -

$ ("[customattr]") मैचों विशेषता "customattr" के साथ सभी टैग

नाम स्थान के साथ

तरह जिम्मेदार बताते हैं 'कस्टम: attr' अपने काम नहीं कर रहा

यहाँ आप एक अच्छा सिंहावलोकन पा सकते हैं: http://www.pamaya.com/jquery-selectors-and-attribute-selectors-reference-and-examples/

+0

लिंक किया गया आलेख कस्टम विशेषताओं का संदर्भ नहीं देता है। –

+2

यह एक उत्तर नहीं है। यह मूल रूप से सिर्फ प्रश्न को बहाल कर रहा है और कह रहा है कि चयनकर्ता नामस्थानों के साथ काम नहीं कर रहे हैं। – spaaarky21

2

यहां एक कस्टम चयनकर्ता का कार्यान्वयन है जो मेरे लिए काम करता है।

// Custom jQuery selector to select on custom namespaced attributes 
$.expr[':'].nsAttr = function(obj, index, meta, stack) { 

    // if the parameter isn't a string, the selector is invalid, 
    // so always return false. 
    if (typeof meta[3] != 'string') 
     return false; 

    // if the parameter doesn't have an '=' character in it, 
    // assume it is an attribute name with no value, 
    // and match all elements that have only that attribute name. 
    if (meta[3].indexOf('=') == -1) 
    { 
     var val = $(obj).attr(meta[3]); 
     return (typeof val !== 'undefined' && val !== false); 
    } 
    // if the parameter does contain an '=' character, 
    // we should only match elements that have an attribute 
    // with a matching name and value. 
    else 
    { 
     // split the parameter into name/value pairs 
     var arr = meta[3].split('=', 2); 
     var attrName = arr[0]; 
     var attrValue = arr[1]; 

     // if the current object has an attribute matching the specified 
     // name & value, include it in our selection. 
     return ($(obj).attr(attrName) == attrValue); 
    } 
}; 

उदाहरण उपयोग:

// Show all divs where the custom attribute matches both name and value. 
$('div:nsAttr(MyNameSpace:customAttr=someValue)').show(); 

// Show all divs that have the custom attribute, regardless of its value. 
$('div:nsAttr(MyNameSpace:customAttr)').show(); 
संबंधित मुद्दे