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>