写法1 代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div>
<h2>原生实现计时器</h2>
<button class="add">+1</button>
<font class="view">0</font>
<button class="subtract">-1</button>
</div>
<div id="app"></div>
</body>
<script type="application/javascript">
const add = document.querySelector(".add");
const view = document.querySelector(".view");
const subtract = document.querySelector(".subtract");
let count = 0;
view.innerHTML = count;
add.addEventListener("click", () => {
count = count + 1;
view.innerHTML = count;
});
subtract.addEventListener("click", () => {
count = count - 1;
view.innerHTML = count;
});
</script>
<script src="../js/vue.js"></script>
<script>
Vue.createApp({
template: `<h2>vue实现计时器</h2>
<button @click='add'>+1</button>
<font>{{count}}</font>
<button @click='subtract'>-1</button>`,
data: function() {
return {
count: 0
}
},
methods: {
add(){
this.count++;
console.log('点击了+1');
},
subtract(){
this.count--;
console.log('点击了-1');
}
}
}).mount('#app');
</script>
</html>
写法2 代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div>
<h2>原生实现计时器</h2>
<button class="add">+1</button>
<font class="view">0</font>
<button class="subtract">-1</button>
</div>
<div id="app"></div>
</body>
<script type="application/javascript">
const add = document.querySelector(".add");
const view = document.querySelector(".view");
const subtract = document.querySelector(".subtract");
let count = 0;
view.innerHTML = count;
add.addEventListener("click", () => {
count = count + 1;
view.innerHTML = count;
});
subtract.addEventListener("click", () => {
count = count - 1;
view.innerHTML = count;
});
</script>
<script src="../js/vue.js"></script>
<script type="x/template" id='tem'</script>
<h2>vue实现计时器</h2>
<button @click='add'>+1</button>
<font>{{count}}</font>
<button @click='subtract'>-1</button>
<script>
Vue.createApp({
template: `#tem`,
data: function() {
return {
count: 0
}
},
methods: {
add(){
this.count++;
console.log('点击了+1');
},
subtract(){
this.count--;
console.log('点击了-1');
}
}
}).mount('#app');
</script>
</html>
写法3 代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div>
<h2>原生实现计时器</h2>
<button class="add">+1</button>
<font class="view">0</font>
<button class="subtract">-1</button>
</div>
<div id="app"></div>
<template id="tem">
<h2>vue实现计时器</h2>
<button @click='add'>+1</button>
<font>{{count}}</font>
<button @click='subtract'>-1</button>
</template>
</body>
<script type="application/javascript">
const add = document.querySelector(".add");
const view = document.querySelector(".view");
const subtract = document.querySelector(".subtract");
let count = 0;
view.innerHTML = count;
add.addEventListener("click", () => {
count = count + 1;
view.innerHTML = count;
});
subtract.addEventListener("click", () => {
count = count - 1;
view.innerHTML = count;
});
</script>
<script src="../js/vue.js"></script>
<script>
Vue.createApp({
template: `#tem`,
data: function() {
return {
count: 0
}
},
methods: {
add(){
this.count++;
console.log('点击了+1');
},
subtract(){
this.count--;
console.log('点击了-1');
}
}
}).mount('#app');
</script>
</html>
|