JavaScript composition and strings

Keywords: Javascript ECMAScript

1. Get element method 2

You can use the getElementsByTagName method on the built-in object document to get a certain label on the page. What you get is a selection set, not an array, but you can use the subscript method to operate the label elements in the selection set

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>obtain</title>
<script type="text/javascript">

window.onload = function(){
    var oUl = document.getElementById('list');/*Avoid getting all global li*/
    var aList = oUl.getElementsByTagName('li'); /*Get elements from tags*/
    var iLen = aList.length;
    for(i=0;i<iLen;i++){
        if(i%2==0){              /*Next execution*/
            aList[i].style.backgroundColor = 'gold';
        }
    }
}
</script>

</head>

<body>

<ul id="list">
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
    <li>6</li>
    <li>7</li>
</ul>

</body>
</html>

2.JavaScript composition

(1) syntax of ECMAscript JavaScript (syntax of variables, functions, loop statements, etc.)

(2) method of DOM document object model to operate HTML and css (document)

(3) BOM browser object model, some methods of browser operation (alert)

3. String processing method

(1) string merge operation: + "

(2) parseInt() converts numeric characters to integers

(3) parseFloat() converts a numeric string to a decimal

(4) split () divides a string into an array of strings

(5) charAt() gets a character in the string

(6) indexOf() to find whether a string contains a character

(7) substring() truncation string usage: substring (start, end) (excluding end)

(8) capitalization of toUpperCase() string

(9) to lowercase() string

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>string</title>
<script type='text/javascript'>

var iNum =123;
var iString = '456';
/*Combination of numbers and characters*/
alert(iNum+iString);

var sTr01 = '12.465';
/*Convert numeric characters to integers*/
alert(parseInt(sTr01));
/*Convert numeric characters to decimals*/
alert(parseFloat(sTr01));

var sTr02 = '2018-1-5';
/*Divide the string into several strings with '-', and divide each character with 'empty character*/
var atime = sTr02.split('-');
alert(atime);

var sTr03 = '#div'
/*Get string of position one*/
var sTr04 = sTr03.charAt(0);
alert(sTr04);

var sTr05 = 'I love you!';
/*Retrieve a character and return its position. The first three are all 2. Return - 1 does not exist*/
var iNum01 = sTr05.indexOf('love');
var iNum02 = sTr05.indexOf('l');
var iNum03 = sTr05.indexOf('lo');
var iNum04 = sTr05.indexOf('hahah');
alert(iNum04);

var sTr05 = "We don't talk anymore!";
/*Excluding the eighth digit, showing 3-7 digits*/
var sTr06 = sTr05.substring(3,8);
alert(sTr06);

/*Convert to uppercase, toLowerCase is lowercase*/
var sTr07 = sTr05.toUpperCase();
alert(sTr07);

var sTr08 = 'dfjkjfahfs13346575431646';
/*Split, reverse, connect*/
var sTr09 = sTr08.split('').reverse().join('');
alert(sTr09);

</script>

</head>

<body>
</body>
</html>

Posted by hannnndy on Sun, 01 Dec 2019 16:00:24 -0800