/ / एक छोटी जेएस लाइब्रेरी के साथ मुद्दे - जावास्क्रिप्ट, एचटीएमएल 5, फ़ंक्शन, डोम

एक छोटी जेएस पुस्तकालय के साथ मुद्दों - जावास्क्रिप्ट, एचटीएमएल 5, समारोह, डोम

मैं एक छोटा जेएस सत्यापन लिखने की कोशिश कर रहा हूंपुस्तकालय, मनोरंजन के लिए और जेएस सीखने के लिए। यह विचार प्रपत्र टैग में तत्वों के माध्यम से लूप करने के लिए है, और यह जांचने के लिए कि अन्य कस्टम विशेषताओं के आधार पर इनपुट तत्व मान्य है या नहीं।

मैं अब उसी "प्रोटोटाइप" में एक फ़ंक्शन को कॉल करने के लिए एक तत्व का उपयोग करने के तरीके पर अटक गया हूं

यह एक ट्यूटोरियल पर आधारित है जिसे मैं विकसित करने की कोशिश कर रहा हूं, मुझे बताएं कि क्या एसई नीति को इस कोड के स्रोत का उल्लेख करने की आवश्यकता है

इस फंक्शन का उपयोग करके html डॉक से कोड बुलाया जाएगा

<script type="text/javascript">
function processForm() {
_("form1").validate();
}
</script>

यह लिबास कोड है:

function _(id) {
if (id) {
if (window === this) {
return new _(id);
}

// We"re in the correct object scop:
// Init our element object and return the object
this.e = document.getElementById(id);
return this;
} else {
return "NO ID PARAM WAS GIVEN";
}
}
_.prototype = {
validate    :function () {
try {
var elem = this.e.elements;
for(var i = 0; i < elem.length; i++){
//alert(elem[i].getAttribute("id"));
// STUCK HERE, how to call the bgcolor function of this prototype
so i can change the bgcolor for the current elem of the loop ?
}
}
catch(err)
{
alert(err.message);
}
},

bgcolor: function (color) {
this.e.style.background = color;
return this;
},
};

उत्तर:

उत्तर № 1 के लिए 1

संभवतः कुछ इस तरह:

for (var i = 0; i < elem.length; i++){
this.bgcolor(elem[i], "red");
}

तथा

bgcolor: function (el, color) {
el.style.background = color;
return this;
}

या शायद एक वैकल्पिक तत्व जो चूक करता है this, अपने मौजूदा कोड के साथ सिंक में रखते हुए जो फॉर्म पर ही काम करता है।

bgcolor: function (color, el) {
el = el || this;
el.style.background = color;
return this;
}