简体   繁体   English

如何从 vue 中的跨度 html 中获取值?

[英]How can I take a value from a span html in vue?

Hello gangs I have a bit problem I have span links in my html file and I need create a filter that when I click in a span this span send a value in my vue file and this value turn on a filter.How I can do this?你好帮派我有一点问题我的 html 文件中有跨度链接,我需要创建一个过滤器,当我单击跨度时,这个跨度会在我的 vue 文件中发送一个值,并且这个值打开一个过滤器。我该怎么做?

Actually I take information for fill the span value from a local array实际上,我从本地数组中获取信息以填充跨度值

<a class="badge badge-pill badge-white-soft  mr-1 mb-1"href="#"
            data-toggle="pill"
            data-target="#portfolio"
            v-for="country in countries" v-bind:key="country " v-onclick="getvalue"
          >
            <span class="h6 text-uppercase">{{ country}}</span>
          </a>  

 data(){ countries:["Africa","America","Asia","Europe"]}

First things first, you have an error in your syntax, it should be v-on:click not v-onclick .首先,您的语法有错误,它应该是v-on:click而不是v-onclick Also if that continues to not work, the capture modifier, v-on:click.capture , may be necessary since you're trying to operate on a inner element of the div.此外,如果这仍然不起作用,则可能需要捕获修饰符v-on:click.capture ,因为您正在尝试对 div 的内部元素进行操作。

As for dynamic values for your span, you most likely want to use v-model .至于跨度的动态值,您很可能希望使用v-model For more information on binding data to your components, refer to the components documentation here.有关将数据绑定到组件的更多信息,请参阅此处的组件文档。 Also, refer to this link for additional information regarding event handling.此外,有关事件处理的更多信息,请参阅此链接

Why don't you just pass the country value to the function?为什么不直接将国家值传递给 function? That will be the clean way.那将是干净的方式。

<a class="badge badge-pill badge-white-soft  mr-1 mb-1"
   href="#"
   data-toggle="pill"
   data-target="#portfolio"
   v-for="country in countries" v-bind:key="country " v-on:click="getvalue(country)"
   >
   <span class="h6 text-uppercase">{{ country}}</span>
</a>  
...

methods: {
  ...
  getValue(v){
    console.log(v)
  }
}

You just need to send the country value on clicking the a tag.您只需要在单击a标签时发送country /地区值。

<a class="badge badge-pill 
     badge-white-soft  mr-1 mb-1"href="#" data-toggle="pill"
     data-target="#portfolio"
     v-for="country in countries" v-bind:key="country " 
     @click="getvalue(country)">
            <span class="h6 text-uppercase">{{ country}}</span>
</a>

Inside the method, you will able to console it在该方法中,您将能够对其进行控制台操作

getValue(country){
  console.log(country)
}

You can also use the span for click by removing it from a tag and您还可以通过a标签中删除span来使用点击,然后

<span class="h6 text-uppercase" @click="getvalue(country)">{{ country}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM