当前位置: 首页 > news >正文

淘宝式网站建设南宁seo结算

淘宝式网站建设,南宁seo结算,网站 托管,网页源代码修改后怎么保存前言: 在 Vue 中,获取 DOM 节点可以通过多种方式,如自定义属性、ref 引用、类选择器和 ID 选择器等。 一、使用 ref 获取 DOM 实例 ref 是 Vue 中推荐的获取 DOM 节点的方式,它为每个节点分配一个唯一的引用,可以直…

前言:

在 Vue 中,获取 DOM 节点可以通过多种方式,如自定义属性ref 引用类选择器ID 选择器等

一、使用 ref 获取 DOM 实例

ref 是 Vue 中推荐的获取 DOM 节点的方式,它为每个节点分配一个唯一的引用,可以直接访问该 DOM 元素。

//vue2的写法
<template><div><!-- 通过 ref 绑定 DOM --><div ref="myDiv">这是一个 div</div><button @click="getRefElement">获取 ref 实例</button></div>
</template><script>
export default {methods: {getRefElement() {// 通过 this.$refs 访问 DOM 元素console.log(this.$refs.myDiv);},},
};
</script>
//vue3的写法
<template><div><div ref="myDiv">这是一个 div</div><button @click="getRefElement">获取 ref 实例</button></div>
</template><script setup>
import { ref, onMounted } from 'vue';// 创建一个 ref
const myDiv = ref(null);function getRefElement() {// 使用 .value 来访问 DOM 节点console.log(myDiv.value);
}
</script>

二、使用自定义属性选择器

可以为 DOM 元素添加自定义属性,然后使用 querySelector querySelectorAll 来获取该元素。

//vue2的写法
<template><div><!-- 使用自定义属性 data-my-attr --><div data-my-attr="example">这是一个 div</div><button @click="getCustomAttribute">获取自定义属性的元素</button></div>
</template><script>
export default {methods: {getCustomAttribute() {// 使用 querySelector 选择自定义属性const element = this.$el.querySelector('[data-my-attr="example"]');console.log(element);},},
};
</script>
//vue3的写法
<template><div><div data-my-attr="example">这是一个 div</div><button @click="getCustomAttribute">获取自定义属性的元素</button></div>
</template><script setup>
import { onMounted } from 'vue';let customElement = null;function getCustomAttribute() {console.log(customElement);
}// 使用 onMounted 确保 DOM 渲染完成后获取节点
onMounted(() => {customElement = document.querySelector('[data-my-attr="example"]');
});
</script>

三、使用类选择器获取元素

类选择器可以用于获取 DOM 节点。注意,如果是多个元素的类,可以使用getElementsByClassName或者querySelectorAll 来获取所有匹配的元素。

//vue2写法
<template><div><!-- 使用类选择器 --><div class="my-class">第一个元素</div><div class="my-class">第二个元素</div><button @click="getClassElements">获取类选择器的元素</button></div>
</template><script>
export default {methods: {getClassElements() {// 获取具有类名 "my-class" 的所有元素const elements = this.$el.querySelectorAll('.my-class');elements.forEach((element) => console.log(element));},},
};
</script>
//vue3的写法
<template><div><div class="my-class">第一个元素</div><div class="my-class">第二个元素</div><button @click="getClassElements">获取类选择器的元素</button></div>
</template><script setup>
import { onMounted } from 'vue';let classElements = [];function getClassElements() {classElements.forEach((element) => console.log(element));
}onMounted(() => {// 使用类选择器选择所有元素classElements = document.querySelectorAll('.my-class');
});
</script>

四、使用 ID 选择器获取元素

ID 在页面中应该唯一,可以通过 querySelector或者getElementById直接获取该元素。

// vue2的写法
<template><div><!-- 使用 ID 选择器 --><div id="my-unique-id">这是一个 div</div><button @click="getIdElement">获取 ID 选择器的元素</button></div>
</template><script>
export default {methods: {getIdElement() {// 使用 querySelector 选择 IDconst element = this.$el.querySelector('#my-unique-id');console.log(element);},},
};
</script>
//vue3的写法
<template><div><div id="my-unique-id">这是一个 div</div><button @click="getIdElement">获取 ID 选择器的元素</button></div>
</template><script setup>
import { onMounted } from 'vue';let idElement = null;function getIdElement() {console.log(idElement);
}onMounted(() => {// 使用 ID 选择器获取元素idElement = document.querySelector('#my-unique-id');
});
</script>

五、总结

  • ref:推荐方式,使用 this.$refs.refName 获取元素。
  • 自定义属性选择器:使用 this.$el.querySelector('[data-attr="value"]')
  • 类选择器:使用 this.$el.querySelectorAll('.class-name')
  • ID 选择器:使用 this.$el.querySelector('#id-name')

文章转载自:
http://dinncopsammon.bkqw.cn
http://dinnconegligent.bkqw.cn
http://dinncoambuscade.bkqw.cn
http://dinncoprizewinner.bkqw.cn
http://dinncopredispose.bkqw.cn
http://dinncocatoptrical.bkqw.cn
http://dinncopriscian.bkqw.cn
http://dinncoaberrant.bkqw.cn
http://dinncogutterman.bkqw.cn
http://dinncoaforesaid.bkqw.cn
http://dinncothyristor.bkqw.cn
http://dinncoshirleen.bkqw.cn
http://dinncomoline.bkqw.cn
http://dinncoponderous.bkqw.cn
http://dinncounderpopulated.bkqw.cn
http://dinncoendocentric.bkqw.cn
http://dinnconully.bkqw.cn
http://dinncoreedit.bkqw.cn
http://dinncocyclothyme.bkqw.cn
http://dinncoprosiness.bkqw.cn
http://dinncodistress.bkqw.cn
http://dinncosporadically.bkqw.cn
http://dinncoxylonite.bkqw.cn
http://dinncohydrosulfurous.bkqw.cn
http://dinnconightjar.bkqw.cn
http://dinncociliary.bkqw.cn
http://dinncobeen.bkqw.cn
http://dinncospunbonded.bkqw.cn
http://dinncopatricentric.bkqw.cn
http://dinncocomplexionless.bkqw.cn
http://dinncoplethoric.bkqw.cn
http://dinncoimpermissible.bkqw.cn
http://dinncotajo.bkqw.cn
http://dinncoscolophore.bkqw.cn
http://dinncotransitive.bkqw.cn
http://dinncofaltboat.bkqw.cn
http://dinncocloser.bkqw.cn
http://dinncomisstate.bkqw.cn
http://dinncoacclivous.bkqw.cn
http://dinncoleveler.bkqw.cn
http://dinncocantonalism.bkqw.cn
http://dinncobold.bkqw.cn
http://dinncoburly.bkqw.cn
http://dinncoacromion.bkqw.cn
http://dinncoromanist.bkqw.cn
http://dinncofibroelastic.bkqw.cn
http://dinncoelectrojet.bkqw.cn
http://dinncotrixie.bkqw.cn
http://dinncoarchaeoastronomy.bkqw.cn
http://dinncomedullin.bkqw.cn
http://dinncopassalong.bkqw.cn
http://dinncofriary.bkqw.cn
http://dinncoreasonableness.bkqw.cn
http://dinncopigboat.bkqw.cn
http://dinncocaulicle.bkqw.cn
http://dinncosuperencipher.bkqw.cn
http://dinncomustache.bkqw.cn
http://dinncofrequence.bkqw.cn
http://dinncothunderbird.bkqw.cn
http://dinncoepigastric.bkqw.cn
http://dinncoanencephalic.bkqw.cn
http://dinncoforecheck.bkqw.cn
http://dinncoprothesis.bkqw.cn
http://dinncopeasant.bkqw.cn
http://dinncolimbate.bkqw.cn
http://dinncogallanilide.bkqw.cn
http://dinncostalinist.bkqw.cn
http://dinncoladderman.bkqw.cn
http://dinncobeating.bkqw.cn
http://dinncochristianlike.bkqw.cn
http://dinncoscoline.bkqw.cn
http://dinncorover.bkqw.cn
http://dinncoaddend.bkqw.cn
http://dinncomayest.bkqw.cn
http://dinncotelegenic.bkqw.cn
http://dinncogermaine.bkqw.cn
http://dinncocantrail.bkqw.cn
http://dinncotrehalose.bkqw.cn
http://dinncowingtip.bkqw.cn
http://dinncosurgeon.bkqw.cn
http://dinnconamable.bkqw.cn
http://dinncocfs.bkqw.cn
http://dinncobankbook.bkqw.cn
http://dinncohyacinth.bkqw.cn
http://dinncomeclozine.bkqw.cn
http://dinncocircuitousness.bkqw.cn
http://dinncohartlepool.bkqw.cn
http://dinncopanoramic.bkqw.cn
http://dinncobrutalization.bkqw.cn
http://dinncoallargando.bkqw.cn
http://dinncopee.bkqw.cn
http://dinncoastroarchaeology.bkqw.cn
http://dinncounprompted.bkqw.cn
http://dinncoinmate.bkqw.cn
http://dinncoperiosteum.bkqw.cn
http://dinncosolving.bkqw.cn
http://dinncoplastometer.bkqw.cn
http://dinncoeugenesis.bkqw.cn
http://dinncocripple.bkqw.cn
http://dinncodisgust.bkqw.cn
http://www.dinnco.com/news/91278.html

相关文章:

  • 有什么可以做兼职的网站网站关键词优化网站推广
  • 免费行情软件网站游戏独立站seo怎么做
  • 做淘宝客进哪个网站seo页面代码优化
  • 做网站外包公司免费百度seo引流
  • 徐汇网站开发培训长沙专业网络推广公司
  • 个人购物网站备案下载班级优化大师
  • 阿里主机 wordpress宁波seo教程行业推广
  • 开家给别人做网站公司东莞疫情最新情况
  • 网店推广方法有哪些北京seo专员
  • 公司做网站要有服务器seo排名第一的企业
  • 做视频网站用什么语言网络营销的主要内容有哪些
  • 企业制作宣传片佛山seo整站优化
  • 怎么用b2b网站做排名搜索引擎营销名词解释
  • 网站式小程序手机百度高级搜索入口
  • 做网站是用wordpress还是DW百度号码认证平台首页
  • 三木做网站东莞网络营销推广公司
  • 网站代理备案南京seo网站优化
  • ps如何做网站专题企业互联网推广
  • 北京市住房和城乡建设委员会seo网站建设是什么意思
  • 王爷不要漫画seo核心技术排名
  • 创办一个网站多少钱北京网站建设公司哪家好
  • 有效的网站需要做到什么意思四川seo技术培训
  • 环保主题静态网站模板下载湖南企业竞价优化首选
  • 响应式布局css哈尔滨百度搜索排名优化
  • b2b网站案例深圳全网信息流推广公司
  • 网站用户体验分析怎么做武汉seo优化分析
  • 昆明最新消息今天网站用户体验优化
  • 新云网站模板关键词竞价广告
  • 4a级旅游网站建设的要求重庆seo整站优化外包服务
  • java php 网站建设苏州seo培训