动态获取vue 3中类绑定的元素的ID
我有一个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-我不想从所有元素中删除课程,然后将一个类添加到一个元素中,因为这涉及一个大的循环,我想避免使用。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于HTML内容是静态的,并且您拥有很多具有常见模式的内容,因此您尝试用
&lt; p id =“ index”
用&lt; p:class =“ {'ActiveClass':IDCLICKED == index}“ ID =“ index”
,因此您需要在vscode中按CTRL+H:by
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:by