VUE中操作dom元素的幾種方法(最新推薦)
VUE中操作dom元素
方法一:
訪問子組件實例或子元素
盡管存在 prop 和事件,有的時候你仍可能需要在 JavaScript 里直接訪問一個子組件。為了達到這個目的,你可以通過 ref 這個 attribute 為子組件賦予一個 ID 引用。例如:
<base-input ref="usernameInput"></base-input>
現(xiàn)在在你已經(jīng)定義了這個 ref 屬性的組件里,你可以使用:
this.$refs.usernameInput
來訪問這個 實例,以便不時之需。比如程序化地從一個父級組件聚焦這個輸入框。在剛才那個例子中,該 組件也可以使用一個類似的 ref 提供對內(nèi)部這個指定元素的訪問,例如:
<input ref="input">
甚至可以通過其父級組件定義方法:
methods: {
// 用來從父級組件聚焦輸入框
focus: function () { this.$refs.input.focus() } }
這樣就允許父級組件通過下面的代碼聚焦 里的輸入框:
this.$refs.usernameInput.focus()
當(dāng) ref 和 v-for 一起使用的時候,你得到的 ref 將會是一個包含了對應(yīng)數(shù)據(jù)源的這些子組件的數(shù)組。
$refs 只會在組件渲染完成之后生效,并且它們不是響應(yīng)式的。這僅作為一個用于直接操作子組件的“逃生艙”——你應(yīng)該避免在模板或計算屬性中訪問 $refs。
方法二:
使用自定義指令
// 注冊一個全局自定義指令 v-focus
Vue.directive('focus', { // 當(dāng)被綁定的元素插入到 DOM 中時…… inserted: function (el) { // 聚焦元素 el.focus() } })
如果想注冊局部指令,組件中也接受一個 directives 的選項:
directives: { focus: { // 指令的定義 inserted: function (el) { el.focus() } } }
然后你可以在模板中任何元素上使用新的 v-focus property,如下:
<input v-focus>
擴展:
vue操作dom元素的三種方法介紹和分析
相信大家在做項目的時候,肯定會遇到這樣的問題:我點擊新增按鈕,需要彈出個彈框,然后我點擊對應(yīng)的關(guān)閉按鈕,關(guān)閉彈框,但是新增按鈕和關(guān)閉按鈕操作的是另一個元素,所以需要獲取dom元素進行操控,那么在vue中怎么操作dom呢?
以下是常用的三種方法:
1、jQuery操作dom(推薦指數(shù):★☆☆☆☆):
只要拿jQuery的選擇器,選中相應(yīng)的dom進行操作就可以了,但是大家都知道jQuery獲取元素是查找頁面所有,相當(dāng)于“循環(huán)”所有元素直至找到需要的dom,但是vue是單頁面的,jQuery獲取dom并不只是獲取vue當(dāng)前頁面,而是從根路由開始查找所有,當(dāng)其他頁面出現(xiàn)相同的元素,也會被獲取到,而且jQuery操作的dom,如果是根據(jù)動態(tài)獲取數(shù)據(jù)渲染的,那么寫在mounted里的操作方法將會失效,必須放到updated里,這樣會導(dǎo)致有些操作被執(zhí)行多遍,所以還是不建議在vue中使用jQuery。
2、原生js操作dom(推薦指數(shù):★★★★☆):
原生js獲取dom就很簡單了,例如:根據(jù)id、class、當(dāng)前元素的上一個元素......
3、vue官方方法:ref(推薦指數(shù):★★★★★):
vue中的ref是把當(dāng)前dom元素 “ 抽離出來 ” ,只要通過 this.$refs就可以獲取到(注意this指向),此方法尤其適用于父元素需要操作子組件的dom元素,這也是子傳父傳遞數(shù)據(jù)的一種方法
下面讓我來看個案例:
設(shè)置了一個button按鈕,通過點擊事件,然后彈出 新增的彈框 , 然后點擊 “ × ”的button按鈕,關(guān)閉彈框,此時需要操作彈框的dom元素,通過ref定義一個名字, 然后在方法中通過 this.$refs.名字 獲取對應(yīng)的dom
<template> <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> </template> <script> export default { name: "Index", data(){ return { } }, methods:{ // 點擊新增按鈕,彈出新增數(shù)據(jù)源的彈框 showAddAlert(){ this.$refs.addAlert.style.display = "block"; }, // 點擊 × 關(guān)閉新增數(shù)據(jù)源的彈框 closeAddAlert(){ this.$refs.addAlert.style.display = "none"; }, }, created(){ } } </script> <style scoped> /* 容器 */ .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{ width: 500px; height: 600px; background: #2b2c2f; position: fixed; left: calc(50% - 250px); top: calc(50% - 300px); 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%; height: 550px; overflow-x: hidden; overflow-y: auto; box-sizing: border-box; padding: 0px 30px 20px; } /* 滾動條效果 */ /* 設(shè)置滾動條的樣式 */ .addAlert-content::-webkit-scrollbar { width: 5px; } /* 滾動槽 */ .addAlert-content::-webkit-scrollbar-track { /* -webkit-box-shadow: inset 0 0 6px rgba(40, 42, 44, 1); border-radius: 10px; */ } /* 滾動條滑塊 */ .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>
效果圖:
以上就是vue中操作dom的方法
到此這篇關(guān)于VUE中操作dom元素的幾種方法(最新推薦)的文章就介紹到這了,更多相關(guān)vue操作dom元素內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue實現(xiàn)el-table點選和鼠標(biāo)框選功能的方法
在Vue中我們經(jīng)常需要處理表格數(shù)據(jù),這篇文章主要給大家介紹了關(guān)于vue實現(xiàn)el-table點選和鼠標(biāo)框選功能的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2023-10-10詳解如何將 Vue-cli 改造成支持多頁面的 history 模式
本篇文章主要介紹了詳解如何將 Vue-cli 改造成支持多頁面的 history 模式,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11vue項目出現(xiàn)ERESOLVE could not resolve問題及解決
這篇文章主要介紹了vue項目出現(xiàn)ERESOLVE could not resolve問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10