admin管理员组文章数量:1431435
I am trying to get an array of DOM-Elements in Vue.js. If I had the following HTML structure:
<select onchange="toggleDisability(this);" class="mySelect" id="mySelect1">
</select>
<select onchange="toggleDisability(this);" class="mySelect" id="mySelect2">
</select>
I could get all elements with the mySelect
class with normal JS like:
var arraySelects = document.getElementsByClassName('mySelect');
Now I am trying to get the same thing with Vue $refs
, but I am always getting the last element. it looks like:
<select id="selection-x" ref="Axis" @change="log($event)"></select>
<select id="selection-y" ref="Axis" @change="log($event)"></select>
and
log(selectElement){
var arraySelects = this.$refs['Axis'];
}
Of course there are also options ,so that @change
event gets emitted, but it doesn't do what I want it to. I want to get an array of the elements with the same ref
just like it works in the example above for normal JS, where you are getting an array of select
elements whose class
attribute equals to mySelect
.
P.S. I know ref
should be unique, but how could it be then used for this particular usecase?
I am trying to get an array of DOM-Elements in Vue.js. If I had the following HTML structure:
<select onchange="toggleDisability(this);" class="mySelect" id="mySelect1">
</select>
<select onchange="toggleDisability(this);" class="mySelect" id="mySelect2">
</select>
I could get all elements with the mySelect
class with normal JS like:
var arraySelects = document.getElementsByClassName('mySelect');
Now I am trying to get the same thing with Vue $refs
, but I am always getting the last element. it looks like:
<select id="selection-x" ref="Axis" @change="log($event)"></select>
<select id="selection-y" ref="Axis" @change="log($event)"></select>
and
log(selectElement){
var arraySelects = this.$refs['Axis'];
}
Of course there are also options ,so that @change
event gets emitted, but it doesn't do what I want it to. I want to get an array of the elements with the same ref
just like it works in the example above for normal JS, where you are getting an array of select
elements whose class
attribute equals to mySelect
.
P.S. I know ref
should be unique, but how could it be then used for this particular usecase?
- "... it doesn't do what I want it to." What do you want it to do? – VisioN Commented Jun 25, 2018 at 10:39
- Check my edited version please – Subhan Asadli Commented Jun 25, 2018 at 10:46
- it's no way, but you can custom a directive like npmjs./package/vue-multi-ref – joaner Commented Jun 25, 2018 at 10:50
-
1
best practice is to specify a
ref
for the parent element.this.$refs.selectwrap.children
– joaner Commented Jun 25, 2018 at 10:53 - Thank you Joaner your second answer is exactly what I need – Subhan Asadli Commented Jun 25, 2018 at 13:34
2 Answers
Reset to default 3No. It is not possible with ref
and $refs
. If you wish to do DOM manipulation then, use vue-directive
or directly access DOM from the root element of the ponent like:
Vue.extend({
mounted() {
// Do what you want with your children.
const children = this.$el.querySelectorAll('.mySelect');
}
})
For me the best way to do this was to set a ref on the parent element (thanks joaner in original ment), but then I also needed to run my code in the "updated" hook so my data was loaded before trying to access the dom (I also have a v-if on the same element I want to reference children):
template:
<ul v-if="dataLoaded" ref="eventlist">
<li class="eventItem"></li>
<li class="eventItem"></li>
<li class="eventItem"></li>
</ul>
javascript:
updated() {
let eventItems = this.$refs.eventlist.children
console.log(eventItems)
}
本文标签: javascriptGet an array of DOMElements in vue jsStack Overflow
版权声明:本文标题:javascript - Get an array of DOM-Elements in vue js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745555983a2663188.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论