动态获取vue 3中类绑定的元素的ID

发布于 2025-02-01 23:12:50 字数 721 浏览 4 评论 0原文

我有一个vue文件,其中约有200个

<p id="2">blabla</p>
...

我可以很容易地找到并从id =“ to :class =” {'activeClass':currentId == idClicked}“ ID =” 然后得到这个,

<p :class="{'activeClass': currentId == idClicked}" id="2">blabla</p>
...

我从VUE路由器中获得iDClicked,通过在页面加载过程中检查URL,但是我无法知道currenti> CurrentID或以某种方式传递在类结合中,因为HTML是静态的。

如何在类绑定中动态获取元素的ID?

谢谢

PS-我不想从所有元素中删除课程,然后将一个类添加到一个元素中,因为这涉及一个大的循环,我想避免使用。

PS 正则问题

I have a vue file with about 200 of these

<p id="2">blabla</p>
...

I can easily do find and replace from id=" to :class="{'activeClass': currentId == idClicked}" id="
and get this

<p :class="{'activeClass': currentId == idClicked}" id="2">blabla</p>
...

I get get the idClicked from the vue router, by checking the url during page load, but there is no way for me to know the currentId or pass it somehow in the class binding, because the HTML is static.

How can I dynamically get the element's id inside the class binding ?

Thank you

PS - I dont want to remove classes from all the elements and then add one class to one element, because that involves a big for loop and I want to avoid that.

PS
regex issue
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

维持三分热 2025-02-08 23:12:50

由于HTML内容是静态的,并且您拥有很多具有常见模式的内容,因此您尝试用&lt; p id =“ index”&lt; p:class =“ {'ActiveClass':IDCLICKED == index}“ ID =“ index”,因此您需要在vscode中按CTRL+H:

<p id="(.*)"

by

<p :class="{'activeClass': idClicked == $1 }" id="$1"

”

Since the HTML content is static and you've a lot of content with a common pattern, and you try to replace <p id="index" with <p :class="{'activeClass': idClicked == index }" id="index", so you need to do this in VSCode by pressing Ctrl+H:

<p id="(.*)"

by

<p :class="{'activeClass': idClicked == $1 }" id="$1"

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文