admin管理员组文章数量:1277896
I want to change the classNames of all elements with the class on "link". I am using a method which is called via a click action on the element. I tested out getting a length from the function and that works fine but adding a class does not. Anyone know why?
HTML
<div id="app">
<ul>
<li><a href="#" class="link" @click="myFunc">Link text 1</a></li>
<li><a href="#" class="link" @click="myFunc">Link text 2</a></li>
<li><a href="#" class="link" @click="myFunc">Link text 3</a></li>
</ul>
</div>
JS
var app = new Vue({
el: '#app',
methods: {
myFunc: function(event){
// works
var ElLength = document.getElementsByClassName('link').length;
console.log('ElLength = ' + ElLength);
// does not work
document.getElementsByClassName('link').className += " hullaballoo";
}
}
});
JSFIDDLE
I want to change the classNames of all elements with the class on "link". I am using a method which is called via a click action on the element. I tested out getting a length from the function and that works fine but adding a class does not. Anyone know why?
HTML
<div id="app">
<ul>
<li><a href="#" class="link" @click="myFunc">Link text 1</a></li>
<li><a href="#" class="link" @click="myFunc">Link text 2</a></li>
<li><a href="#" class="link" @click="myFunc">Link text 3</a></li>
</ul>
</div>
JS
var app = new Vue({
el: '#app',
methods: {
myFunc: function(event){
// works
var ElLength = document.getElementsByClassName('link').length;
console.log('ElLength = ' + ElLength);
// does not work
document.getElementsByClassName('link').className += " hullaballoo";
}
}
});
JSFIDDLE
Share Improve this question asked Jan 12, 2017 at 2:43 Clinton GreenClinton Green 9,97723 gold badges72 silver badges109 bronze badges3 Answers
Reset to default 5document.getElementsByClassName('link')
returns you an array-like object of html element, and .className
is an attribute of each element in this object, maybe you can try this:
document.getElementsByClassName('link')[0].className += " hullaballoo";
instead.
You are trying to change the class on all the links with your current code.
What's I'd suggest instead is:
event.currentTarget.classList.toggle('hullaballoo');
event.currentTarget
will always be the link you clicked.
If you want them all "hullaballoo" when one is clicked you can do:
<a v-for="link in 3"
href="#probably-actually-want-a-button"
@click.prevent="allSelected = !allSelected"
:class="{ hullaballoo: allSelected }">
Link {{ link }}
</a>
However, having to do this is not taking advantage of Vue. You're thinking too DOM like. Ideally you'd want to change these classes based on some state your'e in. You'd need to clarify your question more to get a better answer, though.
The accepted answer will return all matching elements in the document.
If you just want to get those in your ponent, you can do like this:
this.$el.getElementsByClassName('link')
https://v2.vuejs/v2/api/#vm-el
本文标签: javascriptclassName not working in VueJS methodStack Overflow
版权声明:本文标题:javascript - className not working in VueJS method - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741256900a2366852.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论