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>
<meta charset="utf-8">
<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;
        if(i%2==0){              /*Next execution*/
            aList[i].style.backgroundColor = 'gold';



<ul id="list">


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>
<meta charset="utf-8">
<script type='text/javascript'>

var iNum =123;
var iString = '456';
/*Combination of numbers and characters*/

var sTr01 = '12.465';
/*Convert numeric characters to integers*/
/*Convert numeric characters to decimals*/

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

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

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');

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

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

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




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