2015-08-27 2 views
7

मैं यह देखना चाहता हूं कि एक HTML डेटा विशेषता सादा जावास्क्रिप्ट का उपयोग कर मौजूद है या नहीं। मैंने नीचे कोड स्निपेट की कोशिश की है, लेकिन यह काम नहीं करता है।सादे जावास्क्रिप्ट के साथ डेटा विशेषता मौजूद है या नहीं?

if(object.getAttribute("data-params")==="undefined") { 
    //data-attribute doesn't exist 
} 
+0

'अगर (typeof obj [" डेटा-विशेषता "]! ==" अपरिभाषित ") {// यहाँ कोड}' –

उत्तर

14

Element.getAttributenull या विशेषता स्ट्रिंग मौजूद होने पर खाली स्ट्रिंग देता है।

आप Element.hasAttribute का उपयोग करेंगे:

if (!object.hasAttribute("data-params")) { 
    // data attribute doesn't exist 
} 

या Element.dataset (यह भी देखें: in operator):

if (!("params" in object.dataset)) { 
    // data attribute doesn't exist 
} 

या यहाँ तक कि

if (!object.getAttribute("data-params")) { 
    // data attribute doesn't exist or is empty 
} 
+1

क्यों के रूप में चयनित जवाब यह नीचे चिह्नित किया गया है? – Doidgey

0

typeof उपयोग करके देखें:

if(typeof object.getAttribute("data-params") === "undefined") { 
    console.log('data-attribute doesn't exist'); 
} 
1

आप cang बस falsy मूल्य की जांच

if(!object.getAttribute("data-params")) { 
    //data-attribute doesn't exist 
} 

कारण getAttribute शून्य या खाली स्ट्रिंग

भी आप object.hasAttribute("data-params") बस का उपयोग कर सकते लौट सकते हैं यह जांचने के लिए कि विशेषता मौजूद है

2

कोड तुम्हें तैनात जीता नहीं है जैसा कि आप उम्मीद करते हैं, काम नहीं करते हैं, आप यहां क्या कर रहे हैं यह जांच कर रहा है कि विशेषता-मूल्य क्या है नामांकित विशेषता ("data-params") "undefined" के बराबर है, जो true वापस लौटाएगी यदि विशेषता data-params="undefined" है।

if (object.getAttribute("data-params") === "undefined") { 
    // the "data-params" attribute-value is exactly "undefined" 
    // note that `getAttribute()` is a 
} 

मैं क्या संदेह है कि आप क्या करना चाहते है:

var typeOfObjectAttribute = typeof object.getAttribute("data-params"); 

if (typeOfObjectAttribute === null || typeOfObjectAttribute === "") { 
    // data-params attribute doesn't exist on that Node. 
} 

ध्यान दें कि – अनुसार (Element.getAttribute() के लिए संदर्भ में, नीचे) मोज़िला डेवलपर नेटवर्क – कहा गया है कि:

getAttribute() तत्व पर एक निर्दिष्ट विशेषता का मान देता है। यदि दिया गया विशेषता मौजूद नहीं है, तो लौटाए गए मान या तो null या "" (खाली स्ट्रिंग) और नरकिप होगा;

यह भी ध्यान देने योग्य है कि getAttribute() जेनेरिक ऑब्जेक्ट्स की बजाय एलिमेंट नोड्स का एक तरीका है।

// here we look to see if the 'params' key is present in the 
// HTMLElement.dataset object of the element, and then invert 
// that result using the '!' operator, to check that the 
// attribute *isn't* present: 
if (!('params' in document.getElementById('elementID').dataset)) { 
    // the data-params attribute is not present. 
} 

संदर्भ::

  • Element.getAttribute()

    संयोग से, तुम भी निम्नलिखित दृष्टिकोण (फिर से, परीक्षण उस विशेषता नहीं सेट है) इस्तेमाल कर सकते हैं।

  • HTMLElement.dataset
  • in operator
  • typeof operator
+0

यह काम नहीं करता है – jollykoshy

0

आप डेटासेट एपीआई का भी उपयोग कर सकते हैं।

HTMLElement.dataset

HTMLElement.dataset केवल पढ़ने के लिए संपत्ति सभी कस्टम डेटा विशेषताओं के लिए (डेटा- *) तत्व पर निर्धारित करते हैं,, उपयोग की अनुमति देता पढ़ने एवं लिखने दोनों मोड में। यह डोमस्ट्रिंग का नक्शा है, प्रत्येक कस्टम डेटा विशेषता के लिए एक प्रविष्टि।

अफसोस की बात यह आईई 10 में काम नहीं करेगी, लेकिन मुझे पूरा यकीन है कि वहां कहीं भी एक चीज है।

यहाँ एक उदाहरण

var containerData \t = document.querySelector('#container').dataset, 
 
    contentData \t = document.querySelector('#content').dataset; 
 

 
// Check if dataset is empty or not. 
 
console.log(Object.keys(containerData).length === 0); 
 
console.log(Object.keys(contentData).length === 0); 
 

 
// Check for specific data 
 
if (containerData.hasOwnProperty('bar')) { 
 
    console.log(containerData.bar); 
 
} 
 

 
// Here is the dataset 
 
console.log(containerData);
<div id="container" data-foo="bar" data-bar="foo"> 
 
    <div id="content"> 
 
    Content 
 
    </div> 
 
</div>

3

अशक्त के खिलाफ जांच कर रहा है भी समाधान सामने आए है।

if (object.getAttribute("data-params") === null) { 
//data attribute doesn't exist 
}else{ 
//data attribute exists 
} 
संबंधित मुद्दे