September 17
JavaScript è un linguaggio di scripting lato client che in genere viene utilizzato per creare pagine Web dinamiche. Uno dei vantaggi di JavaScript è la sua vasta libreria di funzioni built-in e oggetti che i programmatori Web possono utilizzare per creare, manipolare e testo negozio come stringhe. programmatori JavaScript possono utilizzare le proprietà di una stringa, come ad esempio la proprietà length; utilizzare metodi di una stringa, come dividere e giunzione; e l'accesso HTML metodi che restituiscono stringhe con tag HTML wrapper. JavaScript ha anche funzioni che permettono ai programmatori di stringhe di ricerca, cambiano il caso di una stringa e rimuovere i caratteri specifici da stringhe.
1 Aprire un editor di testo e creare un nuovo file chiamato "noNums.html." Aggiungere i tag HTML di base al file:
<Html>
<Head> </ head>
<Body> </ body>
</ Html>
2 Aggiungere un tag "<script>" tra il file "<head>" e "</ head>" tag, impostare "tipo" di attributo del tag del "<script>" a "text / javascript" e chiudere il "</ script > tag ".
<Html>
<Head>
<Script type = "text / javascript">
</ Script>
</ Head>
<Body> </ body>
</ Html>
3 Aggiungere una funzione JavaScript denominata "numbersOnly" tra il file del "<script>" e "</ script>" tag. La funzione "numbersOnly" non ha argomenti. Aggiungere una parentesi graffa aperta ( "{") e una stretta parentesi graffa ( "}") dopo la dichiarazione di funzione per tenere il posto per il codice della funzione.
<Html>
<Head>
<Script type = "text / javascript">
funzione numbersOnly ()
{
}
</ Script>
</ Head>
<Body> </ body>
</ Html>
4 Recuperare il valore stringa di un campo di immissione con l'ID "InputValue" utilizzando la funzione JavaScript "getElementById". Salvare il valore della stringa a una variabile denominata "InputStr".
<Html>
<Head>
<Script type = "text / javascript">
funzione numbersOnly ()
{
InputStr = document.getElementById ( 'InputValue') il valore.;
}
</ Script>
</ Head>
<Body> </ body>
</ Html>
5 Utilizzare la funzione JavaScript "sostituire" per sostituire tutti i numeri ( "0-9") nella variabile "InputStr" con un carattere nullo ( ''). Salvare i risultati della funzione "sostituire" a una variabile denominata "upperStr".
<Html>
<Head>
<Script type = "text / javascript">
funzione numbersOnly ()
{
InputStr = document.getElementById ( 'InputValue') il valore.;
upperStr = inputStr.replace (/ [0-9] + / g, '');
}
</ Script>
</ Head>
<Body> </ body>
</ Html>
6 Scrivere il valore della variabile "upperStr" al campo di input "InputValue" utilizzando la funzione JavaScript "getElementById".
<Html>
<Head>
<Script type = "text / javascript">
funzione numbersOnly ()
{
InputStr = document.getElementById ( 'InputValue') il valore.;
upperStr = inputStr.replace (/ [0-9] + / g, '');
. Document.getElementById ( 'InputValue') value = upperStr;
}
</ Script>
</ Head>
<Body> </ body>
</ Html>
7 Aggiungere un "<input>" di campo tra il file "<body>" e "</ body>" tag HTML. Dare il campo di input l'ID "InputValue" e il testo "1234567abcdefg89hij". Il "<input>" tag HTML non richiede una stretta "<input />" tag.
<Html>
<Head>
<Script type = "text / javascript">
funzione numbersOnly ()
{
InputStr = document.getElementById ( 'InputValue') il valore.;
upperStr = inputStr.replace (/ [0-9] + / g, '');
. Document.getElementById ( 'InputValue') value = upperStr;
}
</ Script>
</ Head>
<Body>
<Input id = valore "InputValue" = "1234567abcdefg89hij"> </ body>
</ Html>
8 Aggiungere un HTML "<tasto>" tag tra il "<body>" e "</ body>" tag. Utilizzare un "onclick ()" evento per richiamare la funzione "numbersOnly" e sostituire il valore nel campo di immissione "InputValue" ad un valore che non contiene numeri. Dare il pulsante il testo "fare clic per rimuovere i numeri dalla stringa" e chiudere il "<button />" tag.
<Html>
<Head>
<Script type = "text / javascript">
funzione numbersOnly ()
{
InputStr = document.getElementById ( 'InputValue') il valore.;
upperStr = inputStr.replace (/ [0-9] + / g, '');
. Document.getElementById ( 'InputValue') value = upperStr;
}
</ Script>
</ Head>
<Body>
<Input id = valore "InputValue" = "1234567abcdefg89hij"> </ body>
<Button onclick = 'numbersOnly ()'> Clicca per rimuovere i numeri dalla stringa </ button>
</ Html>
9 Aprire noNums.html in un browser Web. Fare clic su "Clicca per rimuovere i numeri dalla stringa" tasto e verificare che gli aggiornamenti stringa nel campo di input "InputValue" senza numeri. Digitare una stringa di diverso nel campo di immissione e fare clic sul pulsante.