Home  >  Article  >  Web Front-end  >  JavaScript DOM API knowledge sharing

JavaScript DOM API knowledge sharing

WBOY
WBOYforward
2022-06-29 15:10:352127browse

This article brings you relevant knowledge about javascript, which mainly organizes related issues related to DOM API knowledge cross-talk, including selecting page tags, operating page tag attributes, etc., Let’s take a look at it together, I hope it will be helpful to everyone.

JavaScript DOM API knowledge sharing

[Related recommendations: javascript video tutorial, web front-end]

JavaScript DOM API knowledge sharing. DOM API knowledge sharing DOM API knowledge sharing>JavaScript DOM API knowledge sharing.JavaScript DOM API knowledge sharing Select the page tagDOM API knowledge sharing>

In the DOM, the document page global object, the functions querySelector and querySelectorAll inside are used to select elements. The purpose is achieved by passing in the CSS selector. The selection range is The selector that existed before this function was not found and the return value is null.

let obj = document.querySelector("选择器");

JavaScript DOM API knowledge sharing

If there are multiple selected tags on the page, only the tag that appears on the page for the first time will be selected.

JavaScript DOM API knowledge sharing
If you want to select all these elements, you need to use the querySelectorAll function.

Usage is the same as querySelector.

let var_name = document.querySelectorAll("选择器");

This function will return an object similar to an array. The usage is exactly the same as an array, but in addition, key-value pairs can be added.
JavaScript DOM API knowledge sharing
Expand the elements in the array and you will find many attributes, which are all native properties of the DOM.

JavaScript DOM API knowledge sharing

JavaScript DOM API knowledge sharing.JavaScript DOM API knowledge sharing Manipulate the attributes of page tagsDOM API knowledge sharing>JavaScript DOM API knowledge sharing.JavaScript DOM API knowledge sharing.JavaScript DOM API knowledge sharing EventsDOM API knowledge sharing>

Many JS codes are triggered through "events". For example, mouse movement, mouse scrolling, keyboard input, changing the browser size, etc. will all generate events.

Three elements of an event:

  1. Event source, which HTML element generated the event.
  2. Event types, such as mouse movement, mouse click, window size change, etc.
  3. Event handling method, what js code should be executed when the event occurs.

For example, click event operation code:

let button = document.querySelector("button");button.onclick = function (){
    alert("嘻嘻嘻!");}

Chestnut and running effect:
JavaScript DOM API knowledge sharing

The figure shows an equivalent writing method, but the equivalent writing method will make the structure of the HTML code more complex, so the first writing method in the figure is more recommended.

JavaScript DOM API knowledge sharing.JavaScript DOM API knowledge sharing.JavaScript DOM API knowledge sharing Get the elementDOM API knowledge sharing>

The classification of the operation element: the content of the

  • element.
  • Attributes of the element.
  • The style of the element.

First of all, you can use the innerHTML attribute to get the content inside a tag when operating the content of an element.

//JavaScript DOM API knowledge sharing.选中标签let var_name = document.querySelector();//JavaScript DOM API knowledge sharing.获取内容let content = var_name.innnerHTML;//JavaScript DOM API knowledge sharing.修改内容var_name.innerHTML = 修改内容;

Chestnut:

JavaScript DOM API knowledge sharing
Effect:
JavaScript DOM API knowledge sharing
We found that the same elements are all folded and output, we can click on the developer tools Settings bar to set the expansion output.

JavaScript DOM API knowledge sharing
Above we placed text in the selected tag. What if it is not text? In fact, it is the same. Assuming a list tag is placed inside, the result obtained is the HTML code of the tag inside.
JavaScript DOM API knowledge sharing

JavaScript DOM API knowledge sharing.JavaScript DOM API knowledge sharing.JavaScript DOM API knowledge sharing Modify elementsDOM API knowledge sharing>

We can also modify the content of HTML, such as changing the list to a title.

JavaScript DOM API knowledge sharing
Effect:
JavaScript DOM API knowledge sharing

JavaScript DOM API knowledge sharing.JavaScript DOM API knowledge sharing.JavaScript DOM API knowledge sharing CounterDOM API knowledge sharing>

Based on the knowledge of obtaining and modifying elements, we can implement a simple counter, the basic idea is:

  1. 获取元素内容
  2. 修改元素内容(加一操作)
  3. 写回到元素里面

JavaScript DOM API knowledge sharingJavaScript DOM API knowledge sharing
JavaScript DOM API knowledge sharing:
JavaScript DOM API knowledge sharing
欸,好像与我们的预期有点不一致,原因就是修改元素内容时,拿到的内容是字符串类型的,发生的是拼接JavaScript DOM API knowledge sharing,而不是算术JavaScript DOM API knowledge sharing,所以我们需要进行转换,那如何转换?我们可以使用与java非常类似且同名的一个方法,它就是parseInt,同理如果需要小数那就有parseFloat,注意这里没有parseDouble方法哦!

JavaScript DOM API knowledge sharingJavaScript DOM API knowledge sharing
JavaScript DOM API knowledge sharing:
JavaScript DOM API knowledge sharingJavaScript DOM API knowledge sharing
我们来丰富一下,加一个按钮,可以完成减的功能。

页面JavaScript DOM API knowledge sharing:

    <style>
        #screen{
            width: 88px;
            height: JavaScript DOM API knowledge sharing0px;
            text-align: center;
            line-height: JavaScript DOM API knowledge sharing0px;
            border-radius: JavaScript DOM API knowledge sharingpx;

            background-color: rgb(JavaScript DOM API knowledge sharing00, JavaScript DOM API knowledge sharing00, JavaScript DOM API knowledge sharing00);
        }
        #plus, #sub{
            width: JavaScript DOM API knowledge sharingJavaScript DOM API knowledge sharingpx;
            height: JavaScript DOM API knowledge sharing0px;
            border-radius: JavaScript DOM API knowledge sharing0px;
            margin-top: JavaScript DOM API knowledge sharingpx;
            border: 0px;

            background-color: rgb(JavaScript DOM API knowledge sharing80, JavaScript DOM API knowledge sharing00, JavaScript DOM API knowledge sharingJavaScript DOM API knowledge sharingJavaScript DOM API knowledge sharing);
        }
    </style>
    <p>
        0    </p>
    <button>计数+JavaScript DOM API knowledge sharing</button>
    <button>计数-JavaScript DOM API knowledge sharing</button>

    <script>

    </script>

JavaScriptJavaScript DOM API knowledge sharing:

let plus = document.querySelector("#plus");let sub = document.querySelector("#sub");let plus_func = function() {
    //JavaScript DOM API knowledge sharing.获取值
    let add_btn = document.querySelector("#screen");
    let val = add_btn.innerHTML;
    val = parseInt(val);
    //JavaScript DOM API knowledge sharing.+JavaScript DOM API knowledge sharing
    val = val + JavaScript DOM API knowledge sharing;
    //JavaScript DOM API knowledge sharing.写回
    add_btn.innerHTML = val;}let sub_func = function() {
    //JavaScript DOM API knowledge sharing.获取值
    let sub_btn = document.querySelector("#screen");
    let val =sub_btn.innerHTML;
    val = parseInt(val);
    //JavaScript DOM API knowledge sharing.-JavaScript DOM API knowledge sharing
    val = val - JavaScript DOM API knowledge sharing;
    //JavaScript DOM API knowledge sharing.写回
    sub_btn.innerHTML = val;}plus.onclick = plus_func;sub.onclick = sub_func;

JavaScript DOM API knowledge sharing:

JavaScript DOM API knowledge sharing
但是对于但标签是没有innerHTML属性的,比如input标签,虽然不能通过innerHTML获取属性,但是可以通过value属性获取内容。

页面JavaScript DOM API knowledge sharing:

    <input>
    <button>+JavaScript DOM API knowledge sharing</button>

    <script>

    </script>

JavaScriptJavaScript DOM API knowledge sharing:

let add = document.querySelector("#add");add.onclick = function() {
    //JavaScript DOM API knowledge sharing.获取值
    let add_btn = document.querySelector("#in");
    let val = add_btn.value;
    val = parseInt(val);
    //JavaScript DOM API knowledge sharing.+JavaScript DOM API knowledge sharing
    val = val + JavaScript DOM API knowledge sharing;
    //JavaScript DOM API knowledge sharing.写回
    add_btn.value = val;}

JavaScript DOM API knowledge sharing:
JavaScript DOM API knowledge sharing

JavaScript DOM API knowledge sharing.JavaScript DOM API knowledge sharing.JavaScript DOM API knowledge sharing点击图片切换DOM API knowledge sharing>

我们想实现一个小案例,就是点击一个图片就能切换图片,再点击一次又能够切换回来,我们可以利用DOM来修改元素的属性来实现,在这个案例中,我们只需JavaScript DOM API knowledge sharing点击事件为修改图片的路径,也就是src属性,就可以实现图片的切换。

假设第一张图片的路径是./jee.png,第二张图片的路径是./樱花.png,实现图片切换的基本思路为:

  1. 获取img元素。
  2. JavaScript DOM API knowledge sharingonclick。
  3. 点击事件的细节就是判断路径是否包含ee,包含就将src属性换成./樱花.png,反过来,判断路径是否包含樱花,包含就将src属性换成./jee.png
  4. 可以使用indexOf方法判断是否包含某个字符串。

JavaScriptJavaScript DOM API knowledge sharing:

let img = document.querySelector("img");img.onclick = function() {
    console.log(img.src);
    if (img.src.indexOf("ee") >= 0) {
        img.src = JavaScript DOM API knowledge sharing9;./樱花.pngJavaScript DOM API knowledge sharing9;;
    } else if (img.src.indexOf("樱花")) {
        img.src = JavaScript DOM API knowledge sharing9;./jee.pngJavaScript DOM API knowledge sharing9;
    }}

页面JavaScript DOM API knowledge sharing:

    <style>
        img {
            height: JavaScript DOM API knowledge sharingJavaScript DOM API knowledge sharing0px;
        }
    </style>
    <img  alt="JavaScript DOM API knowledge sharing" >

    <script>

    </script>

JavaScript DOM API knowledge sharing:
jsJavaScript DOM API knowledge sharingJavaScript DOM API knowledge sharing
具体哪些属性可以修改,我们可以使用console.dir函数来获取某个元素DOM API能够操作的全部属性。

JavaScript DOM API knowledge sharing.JavaScript DOM API knowledge sharing.6暂停/播放切换DOM API knowledge sharing>

实现一个按钮,点击之后按钮文字从"播放"变为"暂停",再点击一次,按钮文字从"暂停"变为"播放"。

实现逻辑和切换逻辑是差不多的,具体看JavaScript DOM API knowledge sharing吧:

JavaScriptJavaScript DOM API knowledge sharing:

let play = document.querySelector("#play");play.onclick = function(){
    if (play.value == "播放") {
        play.value = "暂停";
    } else if (play.value = "暂停") {
        play.value = "播放";
    }}

页面JavaScript DOM API knowledge sharing:

    <input>

实现JavaScript DOM API knowledge sharing:

JavaScript DOM API knowledge sharingJavaScript DOM API knowledge sharing

JavaScript DOM API knowledge sharing.JavaScript DOM API knowledge sharing.7全选/取消全选DOM API knowledge sharing>

首先我们需要创建若干个复选框,只有一个全选框,我们选中全选框,其他元素需要被全部选中,一旦其他元素有未选中的,全选也必须是未选中的。

实现的基本思路:

  1. 获取全选框元素,获取其他元素。
  2. 注册全选框的点击事件,检查其他框是否都被选中,如果选中,则全选框也选中,否则全选框不选中。
  3. 对每一个其他复选框JavaScript DOM API knowledge sharing点击事件,并将状态与全选复选框关联。
  4. 每次点击其他框都要检测其他框是否都选中,以确定全选框的状态。

JavaScriptJavaScript DOM API knowledge sharing:

let all = document.querySelector("#all");let gameroles = document.querySelectorAll(".gamerole");//checked属性为checked表示选中状态,为空字符串表示未选中//JavaScript DOM API knowledge sharingall的点击事件all.onclick = function() {
    for (let i = 0; i 

页面JavaScript DOM API knowledge sharing:

    <input>全选 
    <input>胡桃 
    <input>可莉 
    <input>万叶 
    <input>心海 
    <input>锅巴 

实际JavaScript DOM API knowledge sharing:

JavaScript DOM API knowledge sharingJavaScript DOM API knowledge sharing

JavaScript DOM API knowledge sharing.JavaScript DOM API knowledge sharing.8点击文字放大DOM API knowledge sharing>

DOM还可以修改style属性,下面来尝试运用选中元素与修改元素的知识,来实现点击文字放大的一个小案例。

因为CSS中不区分大小写,属性与变量的命名采用脊柱式命名,而JS中-不能用于变量的命名,为了能够将CSS属性与JS变量名匹配,JS使用驼峰的形式表示CSS的属性,例如font-size属性,对应JS的变量名为fontSize

对于文字的放大,我们可以给文本所在的标签注册一个点击事件,每点击一次就将字体大小增大,即修改CSS的font-size属性。

JavaScriptJavaScript DOM API knowledge sharing:

let p = document.querySelector("p");p.onclick = function() {
    //JavaScript DOM API knowledge sharing.获取文字大小属性
    let wordsSize = parseInt(p.style.fontSize);
    console.log("修改前" + wordsSize);
    //JavaScript DOM API knowledge sharing.修改文字大小
    wordsSize += JavaScript DOM API knowledge sharing;
    //JavaScript DOM API knowledge sharing.写回到属性
    p.style.fontSize = wordsSize + "px";
    console.log("修改后" + wordsSize);}

页面JavaScript DOM API knowledge sharing:

    <p dom api knowledge sharing0px>
        我是一段文本    </p>

实现JavaScript DOM API knowledge sharing:
JavaScript DOM API knowledge sharingJavaScript DOM API knowledge sharing

JavaScript DOM API knowledge sharing.JavaScript DOM API knowledge sharing.9实现关灯/开灯(夜间/白间模式切换)DOM API knowledge sharing>

很多情况下一个个修改样式属性太麻烦了,我们也可以直接修改类属性来达到JavaScript DOM API knowledge sharing,可以通过选中元素变量名.className来获取和修改类属性。

由于JavaScript里面的class是一个关键字,因此获取元素的class不能使用class,而需要使用className,多个class属性可以使用classList

JavaScriptJavaScript DOM API knowledge sharing:

let p = document.querySelector(JavaScript DOM API knowledge sharing9;pJavaScript DOM API knowledge sharing9;);let button = document.querySelector(JavaScript DOM API knowledge sharing9;buttonJavaScript DOM API knowledge sharing9;);button.onclick = function(){
    if (p.className == "light") {
        p.className = JavaScript DOM API knowledge sharing9;blackJavaScript DOM API knowledge sharing9;;
        button.innerHTML = JavaScript DOM API knowledge sharing9;开灯JavaScript DOM API knowledge sharing9;;
    } else if (p.className == JavaScript DOM API knowledge sharing9;blackJavaScript DOM API knowledge sharing9;) {
        p.className = JavaScript DOM API knowledge sharing9;lightJavaScript DOM API knowledge sharing9;;
        button.innerHTML = JavaScript DOM API knowledge sharing9;关灯JavaScript DOM API knowledge sharing9;;
    }}

页面JavaScript DOM API knowledge sharing:

    <style>        
        p{
            width: JavaScript DOM API knowledge sharing00px;
            text-align: center;
        }
        .light, .black{
            width: JavaScript DOM API knowledge sharing00px;
            height: JavaScript DOM API knowledge sharing00px;

            text-align: center;
            line-height: JavaScript DOM API knowledge sharing00px;
            font-size: JavaScript DOM API knowledge sharing0px;
        }

        button{
            width: JavaScript DOM API knowledge sharing00px;
            height: JavaScript DOM API knowledge sharing0px;
            font-size: JavaScript DOM API knowledge sharing0px;
            background-color: rgb(JavaScript DOM API knowledge sharing00,JavaScript DOM API knowledge sharing00,JavaScript DOM API knowledge sharing00);
            border: 0cm;
            border-radius: JavaScript DOM API knowledge sharing0px;
        }
        .light{
            background-color: aliceblue;
            color: black;
        }
        .black{
            background-color: black;
            color: aliceblue;
        }
    </style>
    <p>我是一段文本</p>
    <p>
        <button>关灯</button>
    </p>

实际JavaScript DOM API knowledge sharing:
JavaScript DOM API knowledge sharingJavaScript DOM API knowledge sharing

JavaScript DOM API knowledge sharing.JavaScript DOM API knowledge sharing操作页面结点DOM API knowledge sharing>JavaScript DOM API knowledge sharing.JavaScript DOM API knowledge sharing.JavaScript DOM API knowledge sharing新增结点DOM API knowledge sharing>

除了修改元素的属性和内容,我们还可以在页面上添加元素,要添加元素,那就得先新建一个元素,并且还需要依赖一个父元素(已经创建好的),把这个新建的元素插入到父元素中就能实现元素的添加(依赖与DOM树),这个操作也被称为新增页面结点。
新建元素:

let newp = document.createElement("元素标签");

补充元素内容:

newp.属性 = 值;

插入到DOM树:

选中的父元素.appendChild(创建的子元素);

实例:
JavaScriptJavaScript DOM API knowledge sharing:

let cnt = JavaScript DOM API knowledge sharing;let add = document.querySelector("#add");let parent = document.querySelector("#container");add.onclick = function() {
    let newp = document.createElement("p");
    newp.id = "newp" + cnt;
    newp.className = cnt;
    newp.innerHTML = "hello";
    parent.appendChild(newp);
    console.log(newp);
    cnt++;}

页面JavaScript DOM API knowledge sharing:

    <p></p>
    <button>新增节点</button>

实际JavaScript DOM API knowledge sharing:

JavaScript DOM API knowledge sharing6

JavaScript DOM API knowledge sharing.JavaScript DOM API knowledge sharing.JavaScript DOM API knowledge sharing删除结点DOM API knowledge sharing>

删除结点就更容易了,在DOM树上删除结点就行。

删除结点:

获取到的父元素.removeChild(需要删除的子元素);

实例:
JavaScriptJavaScript DOM API knowledge sharing:

let cnt = JavaScript DOM API knowledge sharing;let add = document.querySelector("#add");let parent = document.querySelector("#container");let arr = [];add.onclick = function() {
    let newp = document.createElement("p");
    arr[cnt-JavaScript DOM API knowledge sharing] = newp;
    newp.id = "newp" + cnt;
    newp.className = cnt;
    newp.innerHTML = "hello";
    parent.appendChild(newp);
    console.log(newp);
    cnt++;}let del = document.querySelector("#del");del.onclick = function() {
    cnt--;
    if (cnt > 0) {
        console.log( "成功删除一个元素");
        parent.removeChild(arr[cnt-JavaScript DOM API knowledge sharing]);
    } else {
        console.log( "该父元素已经没有元素可以删除了");
        cnt++;
    }}

页面JavaScript DOM API knowledge sharing:

    <p></p>
    <button>新增节点</button>
    

实际JavaScript DOM API knowledge sharing:
JavaScript DOM API knowledge sharing7

JavaScript DOM API knowledge sharing.综合案例DOM API knowledge sharing>JavaScript DOM API knowledge sharing.JavaScript DOM API knowledge sharing猜数字DOM API knowledge sharing>

目标页面


JavaScript DOM API knowledge sharing.JavaScript DOM API knowledge sharing


猜数字的逻辑我就不赘述了,不过里面需要取随机数,我们可以通过js中的Math.random()函数来获取随机数,该函数生成随机数的范围是[0,JavaScript DOM API knowledge sharing)区间内的一个小数,我们需要的是[JavaScript DOM API knowledge sharing,JavaScript DOM API knowledge sharing00]之间的整数,我们可以乘上JavaScript DOM API knowledge sharing00后向下取整加一就能得到目标区间的数了,实现向下取整的函数是Math.floor(数字)

然后前端页面部分是通过HTML加上CSS弹性布局实现的,交互通过JavaScript DOM实现,下面的JavaScript DOM API knowledge sharing案例也是一样的,就不多说了。

JavaScriptJavaScript DOM API knowledge sharing:

//获取元素let input = document.querySelector("#guess");let ret = document.querySelector("#result");let cnt = document.querySelector("#count");let guessBtn = document.querySelector("#bJavaScript DOM API knowledge sharing");let flash = document.querySelector("#bJavaScript DOM API knowledge sharing");//生成JavaScript DOM API knowledge sharing-JavaScript DOM API knowledge sharing00的随机数//floor表示只取浮点数整数部分let ansNumber = Math.floor(Math.random() * JavaScript DOM API knowledge sharing00) + JavaScript DOM API knowledge sharing;//实现按钮猜数字的点击事件guessBtn.onclick = function(){
    //输入框没有值,表示用户没有输入,不处理
    if(input.value == JavaScript DOM API knowledge sharing9;JavaScript DOM API knowledge sharing9;) {
        return;
    }
    let guessNumber = parseInt(input.value);
    if (guessNumber > ansNumber) {
        ret.innerHTML = "YO!猜大了!";
        ret.style.color = "red";
    } else if (guessNumber <p>页面JavaScript DOM API knowledge sharing:</p><pre class="brush:php;toolbar:false">nbsp;html>
    <meta>
    <meta>
    <meta dom api knowledge sharing.0>
    <title>猜数字</title>

    <style>
        *{
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            background-color: aliceblue;
        }
        hJavaScript DOM API knowledge sharing {
            width: JavaScript DOM API knowledge sharing00%;
            height: JavaScript DOM API knowledge sharing0px;
            text-align: center;
            line-height: JavaScript DOM API knowledge sharing0px;
            color: blueviolet;
            margin: JavaScript DOM API knowledge sharing0px 0;
        }
        .guessp {
            width: JavaScript DOM API knowledge sharing00%;
            height: JavaScript DOM API knowledge sharing0px;
            text-align: center;
            display: flex;
            justify-content: center;
        }
        .guessp #bJavaScript DOM API knowledge sharing{
            height: JavaScript DOM API knowledge sharing0px;
            width: JavaScript DOM API knowledge sharing0%;
            background-color: rgb(JavaScript DOM API knowledge sharing00,JavaScript DOM API knowledge sharing00,JavaScript DOM API knowledge sharingJavaScript DOM API knowledge sharingJavaScript DOM API knowledge sharing);
            color: aliceblue;
            border: 0cm;
            border-radius: JavaScript DOM API knowledge sharingpx;
        }
        .guessp #bJavaScript DOM API knowledge sharing:active{
            background-color: gray;
        }
        .guessp #guess{
            width: JavaScript DOM API knowledge sharing0%;
            height: JavaScript DOM API knowledge sharing0px;
            border: JavaScript DOM API knowledge sharingpx solid rgb(JavaScript DOM API knowledge sharing00,JavaScript DOM API knowledge sharing00,JavaScript DOM API knowledge sharingJavaScript DOM API knowledge sharingJavaScript DOM API knowledge sharing);
            border-radius: JavaScript DOM API knowledge sharingpx;
            text-indent: 0.6em;
            outline: none;
        }
        .guessp #number{
            width: JavaScript DOM API knowledge sharing0%;
            text-align: right;
        }
        .guessp #in {
            width: 70%;
            margin-left: JavaScript DOM API knowledge sharing%;
            text-align: left;
        }
        .cnt, .ret{
            width: JavaScript DOM API knowledge sharing00%;
            height: JavaScript DOM API knowledge sharing0px;
            text-align: center;
        }
        .rev{
            width: JavaScript DOM API knowledge sharing00%;
            height: JavaScript DOM API knowledge sharing0px;
            text-align: center;
        }
        .rev #bJavaScript DOM API knowledge sharing{
            margin: JavaScript DOM API knowledge sharingpx;
            height: JavaScript DOM API knowledge sharing0px;
            width: JavaScript DOM API knowledge sharing00px;
            background-color: rgb(JavaScript DOM API knowledge sharing00,JavaScript DOM API knowledge sharing00,JavaScript DOM API knowledge sharing00);
            color: aliceblue;
            border: 0cm;
            border-radius: JavaScript DOM API knowledge sharingpx;
        }
        .rev #bJavaScript DOM API knowledge sharing:active{
            background-color: gray;
        }
        .ret, .cnt{
            width: JavaScript DOM API knowledge sharing00%;
            height: JavaScript DOM API knowledge sharing0px;
            display: flex;
            justify-content:center;
        }
        .guessp #messr, .guessp #messc {
            width: JavaScript DOM API knowledge sharing0%;
            text-align: right;
        }
        .guessp #result, .guessp #count{
            width: 70%;
            text-align: left;
            margin-left: JavaScript DOM API knowledge sharing%;
        }


    </style>

    <hjavascript dom api knowledge sharing>猜数字游戏</hjavascript>DOM API knowledge sharing>
    <p>
        <span>要猜的数字:</span>
        <span>
            <input dom api knowledge sharing-javascript sharing00>
            <button dom api knowledge sharing>我就要猜它</button>
        </span>
    </p>
    
    <p>
        <span>结果:</span>
        <span>你还没有猜哦!</span>
    </p>
    <p>
        <span>次数:</span>
        <span>0</span>
    </p>
    <p>
        <button dom api knowledge sharing>
            开始/重新开始猜数字        </button>
    </p>


    <script>

    </script>
    

实际JavaScript DOM API knowledge sharing:
JavaScript DOM API knowledge sharing

JavaScript DOM API knowledge sharing.JavaScript DOM API knowledge sharingJavaScript DOM API knowledge sharingDOM API knowledge sharing>

目标页面


JavaScript DOM API knowledge sharing.JavaScript DOM API knowledge sharing


JavaScriptJavaScript DOM API knowledge sharing:

//获取元素let loveBtn = document.querySelector("#submit");let adv = ["深情地", "温柔地", "随便地", "紧张地", "幽默地", "滑稽地", "开心地", ""];//用户点击表白将表白记录显示在表白按钮下面let record = [];let i = 0;loveBtn.onclick = function() {
    //JavaScript DOM API knowledge sharing.获取表白内容
    let inputs = document.querySelectorAll("input");
    let from = inputs[0].value;
    let to = inputs[JavaScript DOM API knowledge sharing].value;
    let message = inputs[JavaScript DOM API knowledge sharing].value;
    //JavaScript DOM API knowledge sharing.如果有一项内天为空,不处理
    if (from == JavaScript DOM API knowledge sharing9;JavaScript DOM API knowledge sharing9; || to == JavaScript DOM API knowledge sharing9;JavaScript DOM API knowledge sharing9; || message == JavaScript DOM API knowledge sharing9;JavaScript DOM API knowledge sharing9;) {
        return;
    }
    //JavaScript DOM API knowledge sharing.汇总表白语言
    let n = adv.length;
    let index = (Math.floor(Math.random() * JavaScript DOM API knowledge sharing000) + JavaScript DOM API knowledge sharing) % n;
    let romAdv = adv[index];
    let loveMess = from + romAdv + "对" + to + "说" + message;
    record[i] = "留言" + (i+JavaScript DOM API knowledge sharing) + ":" + loveMess;
    //JavaScript DOM API knowledge sharing.新建结点,插入表白记录
    let p = document.createElement("p");
    p.innerHTML = record[i];
    p.className = JavaScript DOM API knowledge sharing9;ohJavaScript DOM API knowledge sharing9;;
    let container = document.querySelector(".container");
    container.appendChild(p);
    i++;
    //JavaScript DOM API knowledge sharing.表白完,清空输入框
    for (let i = 0; i 

页面JavaScript DOM API knowledge sharing:

nbsp;html>
    
    
    
    JavaScript DOM API knowledge sharing
    
    

        "真的是"JavaScript DOM API knowledge sharingDOM API knowledge sharing>         

输入后点击提交, 会将信息显示在表白按钮下方

        

这是一个正经的JavaScript DOM API knowledge sharing,这真的不是一个JavaScript DOM API knowledge sharing

        

        

            是谁:             <input>         

        

            向谁:             <input>         

        

            说:             <input>         

        

                     

              

实际JavaScript DOM API knowledge sharing:

JavaScript DOM API knowledge sharing

【相关推荐:javascript视频教程web前端

The above is the detailed content of JavaScript DOM API knowledge sharing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete