attribut/property hinzufügen
Kermit
- javascript
Hi,
wie kann man eine fehlende property hinzufügen?
Hintergrund:
ein Zugriff auf onblur wenn nicht explizit angegeben wurde gibt eine JS Fehler "aElem[i].attributes.onblur has no properties"
Bsp:
<input name="USR" id="USR" value="user0" class="validation">
Fehler biem Aufruf
document.getElementsByClassName("validation").attributes["onblur"].nodeValue
es funktioniert aber wenn aber onblur definiert wurde
<input name="USR" id="USR" value="user0" class="validation" onblur="">
in mein Beispieltext unten habe ich versucht onblur vorher leer zu setzten, geht aber nicht
if ( typeof(aElem[i].attributes["onblur"]) == "undefine" ) {
aElem[i].attributes["onblur"].nodeValue = "";
}
Wie definiert man korrekt ein leeres onblur Attribut so das man anschließend mit JS darauf zugreifen kann?
vielen Dank
Kermit
###################
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Klassen-Selektor</title>
<script src="prototype-1.6.0.2.js" type="text/javascript"></script>
<style type="text/css">
.extra { background-color:#FF99FF }
</style>
<body>
<input name="USR_LOGIN0" id="USR_LOGIN0" value="user0" maxlength="100" style="width: 240px; background-color: rgb(251, 251, 251);" class="validation"><br>
<input name="USR_LOGIN1" id="USR_LOGIN1" value="user1" maxlength="100" style="width: 240px; background-color: rgb(251, 251, 251);" class="extra validation" onfocus="this.style.backgroundColor='#dbf2ff';" onblur="this.style.backgroundColor='#fbfbfb';" type="text"><br>
<input name="USR_LOGIN2" id="USR_LOGIN2" value="user2" maxlength="100" style="width: 240px; background-color: rgb(251, 251, 251);" class="extra validation hinweis3" onfocus="this.style.backgroundColor='#dbf2ff';" onblur="this.style.backgroundColor='#fbfbfb';" type="text"><br>
<input name="USR_LOGIN5" id="USR_LOGIN5" value="user5" maxlength="100" style="width: 240px; background-color: rgb(251, 251, 251);" class="extra" onfocus="this.style.backgroundColor='#dbf2ff';" onblur="this.style.backgroundColor='#fbfbfb';" type="text"><br>
<script type="text/javascript">
console.info(document.getElementsByClassName("validation"));
console.info(document.getElementsByClassName("extra"));
//console.info($$("validation"));
//console.info($$("extra"));
//var aElem = $$("validation");
var aElem = document.getElementsByClassName("validation");
for( var i=0; i<aElem.length; i++ ) {
console.info(aElem[i].className);
if ( typeof(aElem[i].attributes["onblur"]) == "undefine" ) {
aElem[i].attributes["onblur"].nodeValue = "";
}
console.info(typeof(aElem[i].attributes["onblur"].nodeValue));
aElem[i].attributes["onblur"].nodeValue = "alert(this.value);"+aElem[i].attributes["onblur"].nodeValue;
console.info(aElem[i].attributes["onblur"].nodeValue);
}
</script>
</body>
</html>
Wie definiert man korrekt ein leeres onblur Attribut so das man anschließend mit JS darauf zugreifen kann?
Ist das auch mit prototype möglich? kene mich damit nicht so gut aus :-(
vielen Dank
Kermit
Hi,
versuch mal die Methode element.setAttribute(name, value)
Gruß!
Hi,
ein Zugriff auf onblur wenn nicht explizit angegeben wurde gibt eine JS Fehler "aElem[i].attributes.onblur has no properties"
Hat es einen bestimmten Grund, dass du ueber attributes darauf zugreifen moechtest - und nicht einfach elementreferenz.onblur abfragst?
MfG ChrisB