Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    aiuto per comportamento textbox cross browser

    Ciao a tutti!
    Sono di nuovo qui con un problema che riguarda javascript. Vorrei creare una textbox che non prenda il focus al mousedown ma al click. Ho scritto questo codice:

    codice:
    <html>
    	<head>
    	</head>
    	<body>
    		<input type="text" name="tuoTesto" value="qui il tuo testo" id="input">
    		<script type="text/javascript" >
    			var input=document.getElementById('input');
    			function stop(){return false;}
    			input.onmousedown=stop;//niente focus sul mousedown
    			input.onclick=function(){this.focus();}//focus su click
    			input.onfocus=function(){
    							this.onmousedown='';//abilito il mousedown quando l'oggetto ha il focus
    							alert('ho il focus!');
    						  }
    			input.onblur=function(){this.onmousedown=stop;}//disabilito di nuovo il mousedown al blur
    		</script>
    	</body>
    </html>
    Con questo codice vorrei "comandare" il focus, forzandolo al click e invece impedendolo al mousedown.
    Ho provato con FF, l'ultimo Opera e Chrome e non ho avuto problemi, con IE7 non vuole proprio funzionare...
    Non so proprio dove sbaglio, ogni consiglio e' ben accetto.
    Grazie dell'attenzione,
    Luca

  2. #2
    Aggiornamento:
    codice:
    <html>
    	<head>
    	</head>
    	<body>
    		<input type="text" name="tuoTesto" value="qui il tuo testo" id="input" readonly>
    		<script type="text/javascript" >
    			//Sposta il cursore a fine testo
    			var _SetCursorPosition=function(input){
    				if(typeof(input)!='object')
    					return false;
    				var end=input.value.length;
    				if(input.createTextRange){     //IE
    					var range = input.createTextRange(); 
    					range.move("character", end);
    					range.select(); 
    				} else if(input.selectionStart)//standard browser
    					input.setSelectionRange(end, end); 
    				return true;
    			}
    			//Add event cross browser
    			var addEvent=function(obj,evt,fnc){
    				if(typeof(obj)!='object')
    					return false;
    				if(typeof(evt)!='string')
    					return false;
    				if(typeof(fnc)!='function')
    					return false;
    				
    				if(obj.addEventListener)//standard browser
    					obj.addEventListener(evt,fnc,false);
    				else if(obj.attachEvent)//IE
    						return obj.attachEvent('on'+evt,fnc);
    					else obj['on'+evt]=fnc;
    			}
    			//Remove event croos browser
    			var removeEvent=function(obj,evt,fnc){
    				if(obj.removeEventListener)
    					obj.removeEventListener(evt,fnc,false);//standard browser
    				else if(obj.detachEvent)//IE
    					return obj.detachEvent('on'+evt,fnc);
    			}
    			//Stoppa l'azioen di default dell'evento e senza stopparne la propagazione
    			var preventDefault=function(e){
    				if(window.event)
    					e.returnValue=false;
    				else
    					e.preventDefault();
    			}
    			var _focus=function(){
    								this.readOnly=false;
    								this.focus();
    								removeEvent(input,'mousedown',preventDefault);//mousedown attivo quando il focus e' sulla textbox
    								this.onclick='';
    								_SetCursorPosition(this);
                           }
    			var input=document.getElementById('input');
    			addEvent(input,'mousedown',preventDefault);//niente focus al mousedown
    			//focus su click
    			input.onclick=_focus;
    			//reset
    			input.onblur=function(){
    							this.readOnly=true;
    							addEvent(input,'mousedown',preventDefault);//disabilito di nuovo il mousedown
    							this.onclick=_focus;
    						  }
    		</script>
    	</body>
    </html>
    COn questo codice, il focus sulla textbox e' sempre al mousedown pero', diversamente da prima, solo al click la textbox diventa editabile. E' il meglio che sono riuscito a fare, posto nel caso sia utile a qualcuno

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.