前端复习

Web前端 --- HTML = Tag + CSS + JavaScript

Tag --- Content --- 承载内容
CSS --- Display --- 渲染内容(显示)
JS --- Behavior --- 交互式行为
~ ECMAScript - 核心语法
~ BOM - 浏览器对象模型 - window
~ DOM - 文档对象模型 - document

  • 获取元素:getElementById() / getElementsByTagName() / getElementsByClassName() / querySelector() / querySelectorAll() / parentNode / children / previousElementSibling / nextElementSibling / firstElementChild / lastElementChild
  • 删除元素:父节点.removeChild(子节点)
  • 创建元素:document.createElement(标签名) / 父节点.appendChild(子节点) / 父节点.insertBefore(子节点, 参照节点)
  • 修改元素:textContent / innerHTML / setAttribute()

a标签

  • 页面链接:<a href="[图片上传失败...(image-cbeef5-1560159246110)]

http://www.baidu.com" target="_blank">百度一下</a>

  • 锚点链接:<a href="#foo">去顶部</a>
  • 功能链接:<a href="[图片上传失败...(image-4532e8-1560159246110)]

mailto:jackfrued@126.com">联系站长</a>

1. 原生js代码实现

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>动态列表</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        body {
            background-color: #000;
        }
        #app {
            width: 30%;
            margin: 20px auto;
        }
        #fruits>li {
            width: 90%;
            height: 40px;
            background-color: #6cc0ac;
            margin: 3px 0;
            text-align: center;
            color: #fff;
            font-size: 20px;
            list-style-type: none;
            line-height: 40px;
        }
        #fruits>li>a {
            float: right;
            color: #fff;
            text-decoration: none;
            margin-right: 10px;
        }
        #fruits+div {
            margin-top: 20px;
        }
        #fname {
            width: 70%;
            height: 30px;
            border-radius: 8px;
            border: none;
            outline: none;
            font-size: 20px;
            text-align: center;
            vertical-align: middle;
            background-color: #999;
        }
        #ok {
            width: 18%;
            height: 30px;
            background-color: #a74f58;
            color: #fff;
            border: none;
            outline: none;
            font-size: 14px;
            vertical-align: middle;
        }
    </style>
</head>
<body>
    <div id="app">
        <ul id="fruits">
            <li>苹果<a href="">×</a></li>
            <li>香蕉<a href="">×</a></li>
            <li>榴莲<a href="">×</a></li>
            <li>火龙果<a href="">×</a></li>
        </ul>
        <div>
            <input type="text" id="fname">
            <button id="ok">确定</button>
        </div>
    </div>
    <script>
    const ul = document.querySelector('#fruits')
    const fnameInput = document.querySelector('#fname')
    const okBtn = document.querySelector('#ok')
    const anchors = document.querySelectorAll('#fruits a')

    function removeItem(evt) {
        evt.preventDefault()
        let li = evt.target.parentNode
        li.parentNode.removeChild(li)
    }

    function addItem(evt) {
        let fname = fnameInput.value.trim()
        if (fname.length > 0) {
            let li = document.createElement('li')
            li.textContent = fname
            let a = document.createElement('a')
            a.setAttribute('href', '')
            a.textContent = '×'
            a.addEventListener('click', removeItem)
            li.appendChild(a)
            ul.appendChild(li)
        }
        fnameInput.value = ''
        fnameInput.focus()
    }
    
    window.addEventListener('load', (evt) => {
        for (let i = 0; i < anchors.length; i += 1) {
            anchors[i].addEventListener('click', removeItem)
        }

        fnameInput.addEventListener('keydown', (evt) => {
            let code = evt.keyCode || evt.which
            if (code == 13) {
                addItem()
            } 
        })
        
        okBtn.addEventListener('click', addItem)
    })
    </script>
</body>
</html>

2. jQuery实现

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>动态列表</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        body {
            background-color: #000;
        }
        #app {
            width: 30%;
            margin: 20px auto;
        }
        #fruits>li {
            width: 90%;
            height: 40px;
            background-color: #6cc0ac;
            margin: 3px 0;
            text-align: center;
            color: #fff;
            font-size: 20px;
            list-style-type: none;
            line-height: 40px;
        }
        #fruits>li>a {
            float: right;
            color: #fff;
            text-decoration: none;
            margin-right: 10px;
        }
        #fruits+div {
            margin-top: 20px;
        }
        #fname {
            width: 70%;
            height: 30px;
            border-radius: 8px;
            border: none;
            outline: none;
            font-size: 20px;
            text-align: center;
            vertical-align: middle;
            background-color: #999;
        }
        #ok {
            width: 18%;
            height: 30px;
            background-color: #a74f58;
            color: #fff;
            border: none;
            outline: none;
            font-size: 14px;
            vertical-align: middle;
        }
    </style>
</head>
<body>
    <div id="app">
        <ul id="fruits">
            <li>苹果<a href="">×</a></li>
            <li>香蕉<a href="">×</a></li>
            <li>榴莲<a href="">×</a></li>
            <li>火龙果<a href="">×</a></li>
        </ul>
        <div>
            <input type="text" id="fname">
            <button id="ok">确定</button>
        </div>
    </div>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
    <script>
    // 1. $函数的参数是一个函数,该函数是页面加载完成后执行的回调函数
    $(() => {
        function removeItem(evt) {
            evt.preventDefault()
            // 4. $函数的参数是原生JavaScript对象,返回该原生JavaScript对象对应的jQuery对象
            $(evt.target).parent().remove()
        }
        // 2. $函数的参数是选择器字符串,返回对应元素的jQuery对象
        const input = $('#fname')
        $('#fruits a').on('click', removeItem)
        $('#ok').on('click', (evt) => {
            let fname = input.val().trim()
            if (fname.length > 0) {
                $('#fruits').append(
                    // 3. $函数的参数是标签字符串,创建对应的标签元素并返回jQuery对象
                    $('<li>').text(fname).append(
                        $('<a>').attr('href', '').text('×')
                            .on('click', removeItem)
                    )
                )
            }
            input.val('')
            // jQuery对象通过下标运算或get方法可以获得与之对应的原生JavaScript对象
            // input.get(0).focus()
            input[0].focus()
        })
    })
    </script>
</body>
</html>

3. vue实现

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>动态列表</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        body {
            background-color: #000;
        }
        #app {
            width: 30%;
            margin: 20px auto;
        }
        #fruits>li {
            width: 90%;
            height: 40px;
            background-color: #6cc0ac;
            margin: 3px 0;
            text-align: center;
            color: #fff;
            font-size: 20px;
            list-style-type: none;
            line-height: 40px;
        }
        #fruits>li>a {
            float: right;
            color: #fff;
            text-decoration: none;
            margin-right: 10px;
        }
        #fruits+div {
            margin-top: 20px;
        }
        #fname {
            width: 70%;
            height: 30px;
            border-radius: 8px;
            border: none;
            outline: none;
            font-size: 20px;
            text-align: center;
            vertical-align: middle;
            background-color: #999;
        }
        #ok {
            width: 18%;
            height: 30px;
            background-color: #a74f58;
            color: #fff;
            border: none;
            outline: none;
            font-size: 14px;
            vertical-align: middle;
        }
    </style>
</head>
<body>
    <div id="app">
        <ul id="fruits">
            <li v-for="fruit in fruits">
                {{ fruit }}
                <a href="" @click.prevent="removeItem(fruit)">×</a>
            </li>
        </ul>
        <div>
            <input @keydown.enter="addItem()" type="text" id="fname" v-model="fname">
            <button id="ok" @click="addItem()">确定</button>
        </div>
    </div>
    <script src="https://cdn.bootcss.com/vue/2.6.10/vue.min.js"></script>
    <script>
    const app = new Vue({
        el: '#app',
        data: {
            fruits: ['苹果', '香蕉', '榴莲', '火龙果'],
            fname: ''
        },
        methods: {
            addItem() {
                if (this.fname.trim().length > 0) {
                    this.fruits.push(this.fname.trim())
                }
                this.fname = ''
            },
            removeItem(fruit) {
                let index = this.fruits.indexOf(fruit)
                this.fruits.splice(index, 1)
            }
        }
    });
    </script>
</body>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 28,502评论 1 45
  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,712评论 1 92
  • 第一章 1.什么是DOM DOM: Document Object Model(文档对象模型) 是JavaScri...
    fastwe阅读 4,272评论 0 0
  • 概要 64学时 3.5学分 章节安排 电子商务网站概况 HTML5+CSS3 JavaScript Node 电子...
    阿啊阿吖丁阅读 13,177评论 0 3
  • 简述JavaScript起源起源于美国的Netscape公司,原名为LiveScript,后改为JavaScrip...
    3ab670b99521阅读 8,257评论 0 0

友情链接更多精彩内容