vue觸發(fā)真實(shí)的點(diǎn)擊事件跟用戶行為一致問(wèn)題
vue觸發(fā)真實(shí)的點(diǎn)擊事件跟用戶行為一致
<template>
<div>
<button ref="myButton" @click="handleClick">按鈕</button>
</div>
</template>
<script>
export default {
methods: {
handleClick() {
const button = this.$refs.myButton;
// 創(chuàng)建一個(gè)鼠標(biāo)點(diǎn)擊事件
const event = new MouseEvent('click', {
view: window,
bubbles: true,
cancelable: true
});
// 觸發(fā)鼠標(biāo)點(diǎn)擊事件
button.dispatchEvent(event);
}
}
}
</script>在這個(gè)示例中
我們有一個(gè)按鈕,并綁定了一個(gè)點(diǎn)擊事件處理函數(shù) handleClick。
在 handleClick 方法中,我們首先獲取按鈕元素的引用。
然后,使用 new MouseEvent 創(chuàng)建一個(gè)新的鼠標(biāo)點(diǎn)擊事件,可以設(shè)置一些相關(guān)的屬性。
最后,通過(guò)調(diào)用 dispatchEvent 方法并傳遞創(chuàng)建的事件對(duì)象,來(lái)觸發(fā)真實(shí)的鼠標(biāo)點(diǎn)擊事件。
請(qǐng)注意,通過(guò) dispatchEvent 方法觸發(fā)的鼠標(biāo)點(diǎn)擊事件是真實(shí)的,它會(huì)觸發(fā)與用戶實(shí)際點(diǎn)擊按鈕時(shí)相同的行為和效果。
vue實(shí)現(xiàn)自動(dòng)觸發(fā)點(diǎn)擊事件
1.使用VUE自定義指令實(shí)現(xiàn)
<el-tree :check-strictly="isCheck" ref="tree" :data="data" show-checkbox node-key="detectorId" :default-expanded-keys="[]" default-expand-all :default-checked-keys="defKeys" :expand-on-click-node="false" :props="defaultProps" @check-change="handleCheckChange">
<span class="custom-tree-node" slot-scope="{ node, data }">
<span>
<i class="iconfont icon-jianceqi" v-if="data.isDet"></i>
<i v-else style="color:#F19F00;margin-right:10px;" class="iconfont icon-24gf-folder"></i>
{{ node.label }}
<!-- <i class="iconfont fr icon-gengduo1"></i> -->
</span>
<span>
<el-button v-if="data.itemsNumber > 0 && data.isShow" type="text" size="mini" v-trigger class="wlh-textColor-red" @click="() => append(data)">
查看檢測(cè)器
</el-button>
<!-- <el-button
type="text"
size="mini"
@click="() => remove(node, data)">
Delete
</el-button> -->
</span>
</span>
</el-tree> methods(){
append(data) {
this.getDeteListFromGroup(data.organizationId, () => {
this.checkList.forEach(item => {
const newChild = {
organizationId: item.detectorId,
detectorId: item.detectorId,
organizationName: item.detectorName,
children: [],
isDet: true
};
if (!data.children) {
this.$set(data, "children", []);
}
data.children.push(newChild);
this.getPolicyOne();
});
});
// data = Object.assign({},data,{isShow:false})
data.isShow = false;
}
},
2.指令
directives: {
trigger: {
inserted(el, binging) {
// console.log(el)
// el.click()
$(el).trigger('click');//所以都觸發(fā)一次,
}
}
}3.回顯賦值
el-tree的數(shù)據(jù)是動(dòng)態(tài)添加的,所以回顯的數(shù)據(jù)一定要在data數(shù)據(jù)追加后再回顯被選中的數(shù)據(jù)
getInfo(){
this.isCheck = true //重點(diǎn):回顯之前一定要設(shè)置為true
this.$nextTick(() => {
this.$refs.tree.setCheckedKeys(this.defKeys) //給樹(shù)節(jié)點(diǎn)賦值回顯
this.isCheck = false //重點(diǎn): 賦值完成后 設(shè)置為false
})
}
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解如何實(shí)現(xiàn)一個(gè)簡(jiǎn)單的 vuex
本篇文章主要介紹了如何實(shí)現(xiàn)一個(gè)簡(jiǎn)單的 vuex,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
vue+element-ui表格封裝tag標(biāo)簽使用插槽
這篇文章主要介紹了vue+element-ui表格封裝tag標(biāo)簽使用插槽,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
前端vue-cropperjs實(shí)現(xiàn)圖片裁剪方案
這篇文章主要為大家介紹了前端vue-cropperjs實(shí)現(xiàn)圖片裁剪方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
vue中v-for循環(huán)數(shù)組,在方法中splice刪除數(shù)組元素踩坑記錄
這篇文章主要介紹了vue中v-for循環(huán)數(shù)組,在方法中splice刪除數(shù)組元素踩坑記錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
element-ui中導(dǎo)航組件menu的一個(gè)屬性:default-active說(shuō)明
這篇文章主要介紹了element-ui中導(dǎo)航組件menu的一個(gè)屬性:default-active說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
vue實(shí)現(xiàn)簡(jiǎn)單加法計(jì)算器
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)簡(jiǎn)單加法計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10
基于vue組件實(shí)現(xiàn)猜數(shù)字游戲
這篇文章主要為大家詳細(xì)介紹了基于vue組件實(shí)現(xiàn)猜數(shù)字游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11

