VueJS 读取 Dom 个属性
VueJS Read Dom Attributes
我想获取按钮点击事件的href属性。
<a v-on:click.prevent="func($event)" href="/user/all/2">
<i class="fa fa-edit"></i>
<span>Get Data</span>
</a>
Main.JS 个文件
new Vue({
el: 'body',
methods: {
func: function (event) {
element = event.target;
console.log(element); // Output : Select span|i|a element
href = element.getAttribute('href');
},
}
});
目标事件没有 select 元素。它 select 是被点击的元素。
你想要 event.currentTarget
,而不是 event.target
。这是 fiddle 的情况:https://jsfiddle.net/crswll/553jtefh/
这是"Vue way"。 Vue 是关于可重用组件的。所以,首先创建组件:
<script src="https://unpkg.com/vue"></script>
<div id="app">
<my-comp></my-comp>
</div>
<script>
// register component
Vue.component('my-comp', {
template: '<div>Just some text</div>'
})
// create instance
new Vue({
el: '#app'
})
</script>
现在添加自定义属性并读取其值:
<script src="https://unpkg.com/vue"></script>
<div id="app">
<my-comp my-attr="Any value"></my-comp>
</div>
<script>
Vue.component('my-comp', {
template: '<div>aaa</div>',
created: function () {
console.log(this.$attrs['my-attr']) // And here is - in $attrs object
}
})
new Vue({
el: '#app'
})
</script>
var app = {
func: function (event) {
console.log(event.currentTarget.id);//this will get whole html tag
console.log(event.currentTarget.href);//this will show href value
}
}
// Apps
var app_vue = new Vue({
data: app,
}).$mount("#app_vue");
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app_vue" v-cloak class="card" >
<a v-on:click.prevent="func" href="/user/all/2">
<i class="fa fa-edit"></i>
<span>Get Data</span>
</a>
</div>
我想获取按钮点击事件的href属性。
<a v-on:click.prevent="func($event)" href="/user/all/2">
<i class="fa fa-edit"></i>
<span>Get Data</span>
</a>
Main.JS 个文件
new Vue({
el: 'body',
methods: {
func: function (event) {
element = event.target;
console.log(element); // Output : Select span|i|a element
href = element.getAttribute('href');
},
}
});
目标事件没有 select 元素。它 select 是被点击的元素。
你想要 event.currentTarget
,而不是 event.target
。这是 fiddle 的情况:https://jsfiddle.net/crswll/553jtefh/
这是"Vue way"。 Vue 是关于可重用组件的。所以,首先创建组件:
<script src="https://unpkg.com/vue"></script>
<div id="app">
<my-comp></my-comp>
</div>
<script>
// register component
Vue.component('my-comp', {
template: '<div>Just some text</div>'
})
// create instance
new Vue({
el: '#app'
})
</script>
现在添加自定义属性并读取其值:
<script src="https://unpkg.com/vue"></script>
<div id="app">
<my-comp my-attr="Any value"></my-comp>
</div>
<script>
Vue.component('my-comp', {
template: '<div>aaa</div>',
created: function () {
console.log(this.$attrs['my-attr']) // And here is - in $attrs object
}
})
new Vue({
el: '#app'
})
</script>
var app = {
func: function (event) {
console.log(event.currentTarget.id);//this will get whole html tag
console.log(event.currentTarget.href);//this will show href value
}
}
// Apps
var app_vue = new Vue({
data: app,
}).$mount("#app_vue");
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app_vue" v-cloak class="card" >
<a v-on:click.prevent="func" href="/user/all/2">
<i class="fa fa-edit"></i>
<span>Get Data</span>
</a>
</div>