vue操作dom元素的3種方法示例
1.原生js操作dom
const dom = getElementById(‘box')
2.vue官方方法:ref
vue中的ref是把當(dāng)前dom元素 “ 抽離出來(lái) ” ,只要通過(guò) this.$refs就可以獲取到
< div class=“set” ref=“up”>
.set是我們要操作的dom對(duì)象,它的ref是 up
@click=“Alert”
給父元素一個(gè)點(diǎn)擊事件,
接下來(lái)我們來(lái)編寫(xiě)這個(gè)方法
methods:{ this.$refs.addAlert.style.display = “block”; }
CSS還要嗎?
那我把代碼全粘過(guò)來(lái)你們自己看吧
<template> <div id="app"> <div class="index-box"> <!--新增按鈕--> <input type="button" id="DbManagement-addBtn" @click="showAddAlert" value="新增"> <!--新增數(shù)據(jù)源彈框--> <div class="addDbSource-alert" ref="addAlert"> <div class="addAlert-top"> <!--添加數(shù)據(jù)源--> <input type="button" value="×" class="addAlert-close" @click="closeAddAlert"> </div> <div class="addAlert-content"> <div style="height: 1000px;"></div> </div> </div> </div> </div> </template> <script> export default { name: "Index", data(){ return { } }, methods:{ // 點(diǎn)擊新增按鈕,彈出新增數(shù)據(jù)源的彈框 showAddAlert(){ this.$refs.addAlert.style.display = "block"; }, // 點(diǎn)擊 × 關(guān)閉新增數(shù)據(jù)源的彈框 closeAddAlert(){ this.$refs.addAlert.style.display = "none"; }, }, created(){ } } </script> <style scoped> #app{ width:100%; height:100%; overflow-y:auto; } /* 容器 */ .index-box{ width: 100%; height: 100%; background: #212224; display: flex; } /* 添加數(shù)據(jù)源按鈕 */ #DbManagement-addBtn { width: 100px; height: 45px; border: none; border-radius: 10px; background: rgba(29, 211, 211, 1); box-shadow: 2px 2px 1px #014378; margin-left: 20px; margin-top: 17px; cursor: pointer; font-size: 20px; } /*新增數(shù)據(jù)源彈框*/ .addDbSource-alert{ position: fixed; top:0;left:0;right:0;bottom:0; margin:auto; width: 4rem;height: 4rem; background: #2b2c2f; display: none; } /*新增彈框頭部*/ .addAlert-top{ width: 100%; height: 50px; background: #1dd3d3; line-height: 50px; font-size: 20px; box-sizing: border-box; padding-left: 20px; } /*新增彈框關(guān)閉*/ .addAlert-close{ background: #1dd3d3; border: none; font-size: 30px; cursor: pointer; float: right; margin-right: 20px; margin-top: 5px; } /*新增彈框內(nèi)容部分*/ .addAlert-content{ width: 100%; box-sizing: border-box; padding: 0px 30px 20px; } /* 滾動(dòng)條效果 */ /* 設(shè)置滾動(dòng)條的樣式 */ .addAlert-content::-webkit-scrollbar { width: 5px; } /* 滾動(dòng)槽 */ .addAlert-content::-webkit-scrollbar-track { /* -webkit-box-shadow: inset 0 0 6px rgba(40, 42, 44, 1); border-radius: 10px; */ } /* 滾動(dòng)條滑塊 */ .addAlert-content::-webkit-scrollbar-thumb { border-radius: 10px; background: rgba(29, 211, 211, 1); /* -webkit-box-shadow: inset 0 0 6px rgba(29, 211, 211, 1); */ } .addAlert-content::-webkit-scrollbar-thumb:window-inactive { background: rgba(29, 211, 211, 1); } </style>
CSS比正文和腳本加起來(lái)都多,如果你能看懂CSS,沒(méi)理由學(xué)不會(huì) ref
還有第三種方法,jQuery 操作dom,看完以后直呼不敢用
3.jQuery操作dom
只要拿jQuery的選擇器,選中相應(yīng)的dom進(jìn)行操作就可以了,但是大家都知道jQuery獲取元素是查找頁(yè)面所有,相當(dāng)于“循環(huán)”所有元素直至找到需要的dom,但是vue是單頁(yè)面的,jQuery獲取dom并不只是獲取vue當(dāng)前頁(yè)面,而是從根路由開(kāi)始查找所有,當(dāng)其他頁(yè)面出現(xiàn)相同的元素,也會(huì)被獲取到,而且jQuery操作的dom,如果是根據(jù)動(dòng)態(tài)獲取數(shù)據(jù)渲染的,那么寫(xiě)在mounted里的操作方法將會(huì)失效,必須放到updated里,這樣會(huì)導(dǎo)致有些操作被執(zhí)行多遍,所以還是不建議在vue中使用jQuery。
總結(jié)
到此這篇關(guān)于vue操作dom元素的3種方法示例的文章就介紹到這了,更多相關(guān)vue操作dom元素方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue中的無(wú)限加載vue-infinite-loading的方法
本篇文章主要介紹了Vue中的無(wú)限加載vue-infinite-loading的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04vue腳手架搭建/idea執(zhí)行vue項(xiàng)目全過(guò)程
新建目錄并運(yùn)行命令提示符,通過(guò)npm安裝Vue腳手架并查看版本號(hào),接著,使用vue create命令創(chuàng)建Vue項(xiàng)目,選擇所需配置后完成項(xiàng)目創(chuàng)建,創(chuàng)建成功可見(jiàn)Vue文件夾,使用IDEA打開(kāi)項(xiàng)目,并啟動(dòng)項(xiàng)目,根據(jù)需求修改初始化界面2024-10-10Vue引入vuetify框架你需要知道的幾點(diǎn)知識(shí)
這篇文章主要介紹了Vue引入vuetify框架你需要知道的幾點(diǎn)知識(shí),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06Vue項(xiàng)目添加動(dòng)態(tài)瀏覽器頭部title的方法
這篇文章主要介紹了Vue項(xiàng)目添加動(dòng)態(tài)瀏覽器頭部title的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07關(guān)于vue項(xiàng)目proxyTable配置和部署服務(wù)器的問(wèn)題
這篇文章主要介紹了關(guān)于vue項(xiàng)目proxyTable配置和部署服務(wù)器的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04如何在 ant 的table中實(shí)現(xiàn)圖片的渲染操作
這篇文章主要介紹了如何在 ant 的table中實(shí)現(xiàn)圖片的渲染操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10vue進(jìn)行圖片的預(yù)加載watch用法實(shí)例講解
下面小編就為大家分享一篇vue進(jìn)行圖片的預(yù)加載watch用法實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02Vue中data傳遞函數(shù)、props接收函數(shù)及slot傳參的使用及說(shuō)明
這篇文章主要介紹了Vue中data傳遞函數(shù)、props接收函數(shù)及slot傳參的使用及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10如何使用Gitee Pages服務(wù) 搭建Vue項(xiàng)目
這篇文章主要介紹了如何使用Gitee Pages服務(wù) 搭建Vue項(xiàng)目,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10