2013-07-17 3 views
-2

Je suis en train de comprendre cette JS fonction: JS Fiddle Demodétecter la saisie de texte

Je fondamentalement, il est sorti d'un livre que je suis en train d'apprendre. Le livre s'appelle "JavaScript: le guide définitif" (pg484). Mais la fonction n'inclut pas le code html qui va avec. J'apprécierais que quelqu'un puisse m'aider à écrire le code HTML qui ferait ce travail, ainsi je pourrais être en mesure de mieux comprendre comment cela fonctionne. J'ai fait un coup de poignard à ce sujet avec le lien ci-dessus. Je n'aime vraiment pas ce livre comme il le fait. Cela arrive souvent. Je suis un novice, est-ce que quelqu'un a des conseils sur ce qu'il faut faire d'autre que de venir ici et d'essayer d'obtenir une réponse.

Appréciez toute aide.

//Example 17-7. Using the propertychange event to detect text input 
function forceToUpperCase(element) { 
    if (typeof element === "string") element = document.getElementById(element); 
    element.oninput = upcase; 
    element.onpropertychange = upcaseOnPropertyChange; 
    // Easy case: the handler for the input event 
    function upcase(event) { this.value = this.value.toUpperCase(); } 
    // Hard case: the handler for the propertychange event 
    function upcaseOnPropertyChange(event) { 
    var e = event || window.event; 
    // If the value property changed 
    if (e.propertyName === "value") { 
     // Remove onpropertychange handler to avoid recursion 
     this.onpropertychange = null; 
     // Change the value to all uppercase 
     this.value = this.value.toUpperCase(); 
     // And restore the original propertychange handler 
     this.onpropertychange = upcaseOnPropertyChange; 
    } 
    } 
} 
+1

Veuillez prendre le temps de mettre en retrait votre code. Personne ne veut lire ce gâchis. Si tu veux notre aide, rends-la au moins aussi agréable que possible pour nous. – meagar

+0

essayez http://jsfiddle.net/arunpjohny/vP9kD/1/ ou http://jsfiddle.net/arunpjohny/vP9kD/2/ –

+0

Tks Arun, cela m'a été utile. – HattrickNZ

Répondre

0

HTML connexes pourrait être:

<input type="text" id="i0"> 

<script> 
    window.onload = function() { 
    forceToUpperCase('i0') 
    } 
</script> 

Cependant, je ne suis pas sûr de ce que la fonction fait est utile ou que l'attachement des auditeurs et des méthodes de détachement utilisés sont robustes. Mais cela pourrait être utile pour comprendre certains aspects des événements et des auditeurs.

+0

salut RobG, tks pour cela mais ne comprends pas complètement ce que vous faites. – HattrickNZ

+0

La réponse ajoute un élément à la page avec ID * i0 *, puis appelle la fonction * forceToUpperCase * une fois le chargement de la page terminé. La fonction ajoute des écouteurs aux événements * input * et * propertychange * de l'élément. – RobG

0
<!DOCTYPE html> 
<html> 
    <head> 
     <script> 
     var element = document.getElementbyId(Java_C#); 
     function forceToUpperCase(element) { 
      if (typeof element === "string") element = document.getElementById(element);  
       element.oninput = upcase; 
       element.onpropertychange = upcaseOnPropertyChange; 
     // Easy case: the handler for the input event 

     function upcase(event) { this.value = this.value.toUpperCase(); } 
     // Hard case: the handler for the propertychange event 

     function upcaseOnPropertyChange(event) { 
     var e = event || window.event; 

     // If the value property changed 
     if (e.propertyName === "value") { 

     // Remove onpropertychange handler to avoid recursion 
     this.onpropertychange = null; 

     // Change the value to all uppercase 
     this.value = this.value.toUpperCase(); 

     // And restore the original propertychange handler 
     this.onpropertychange = upcaseOnPropertyChange; 
    } 
    } 
} 
     </script> 
    </head> 
<body> 
    <p id="Java_C#"> 
    Public static void main{} 
    </p> 
</body> 
</html> 
+0

tks mais espérait obtenir un exemple de travail avec lequel je pourrais interagir et voir travailler comme Arun l'a fait plus haut. – HattrickNZ

+0

a-t-il été résolu @HattrickNZ – KING