【问题标题】:How do I highlight search queries using Vue+html如何使用 Vue+html 突出显示搜索查询
【发布时间】:2023-03-31 18:43:01
【问题描述】:

这里我的搜索工作正常,但我是新手,不知道如何使用 npm...我正在使用带有 VueJS 的 HTML 来实现这一点,并成功创建了搜索过滤器,但无法实现高亮过滤器。 .一些线索是使用的markjs.io和其他一些自定义vuejs过滤器,但无法实现它们。

class Post {
  constructor(title, link, author, img, course, coursel, cours, coursl, cour, courl, cou, coul, co, col) {
    this.title = title;
    this.link = link;
    this.author = author;
    this.img = img;
    this.course = course;
    this.coursel = coursel;
    this.cours = cours;
    this.coursl = coursl;
    this.cour = cour;
    this.courl = courl;
    this.cou = cou;
    this.coul = coul;
    this.co = co;
    this.col = col;
  }
}
const app = new Vue({
  el: "#app",
  data: {
    search: "",
    postList: [
      new Post("abc", null, "xyz", "wqw", "qwe", "", " ", null, " ", null, " ", null, " ", null, );
    ]
  },
  computed: {
    filteredList() {
      return this.postList.filter(post => {
        return post.title.toLowerCase().includes(this.search.toLowerCase()) ||
          post.author.toLowerCase().includes(this.search.toLowerCase()) || 
          post.course.toLowerCase().includes(this.search.toLowerCase()) || 
          post.cours.toLowerCase().includes(this.search.toLowerCase()) || 
          post.cour.toLowerCase().includes(this.search.toLowerCase()) || 
          post.cou.toLowerCase().includes(this.search.toLowerCase()) || 
          post.co.toLowerCase().includes(this.search.toLowerCase());
      });
    }
  }
});

这是我正在使用的 HTML 代码

<div id="app">
  <div class="search-wrapper">
    <input type="text" v-model="search" placeholder="Search"
      onfocus="if(this.value==this.defaultValue)this.value=''"    
      onblur="if(this.value=='')this.value=this.defaultValue" />
  </div>
  <div class="wrapper">
    <div class="col-lg-12 col-md-4 card" v-for="post in filteredList">
      <a v-bind:href="post.link" target="_blank">
        <img v-bind:src="post.img"/> 
        <div style="line-height:20%;">
          <br>
        </div>
        <font size="2">{{ post.author }}</font>
        <div class="brmedium"></div>
        <font size="5"><b><i><em>{{ post.title }}</em></i></b></font>
      <a v-bind:href="post.coursel" title="View Credential" target="_blank"><font size="3">{{ post.course }}</font></a>
      <a v-bind:href="post.coursl" title="View Credential" target="_blank"><font size="3">{{ post.cours }}</font></a>
      <a v-bind:href="post.courl" title="View Credential" target="_blank"><font size="3">{{ post.cour }}</font></a>
      <a v-bind:href="post.coul" title="View Credential" target="_blank"><font size="3">{{ post.cou }}</font></a>
      <a v-bind:href="post.col" title="View Credential" target="_blank"><font size="3">{{ post.co }}</font></a>
      </a>
    </div>
  </div>
</div>

【问题讨论】:

  • 这和 npm 有什么关系? npm 是一个包管理器。
  • 我还不知道如何使用 npm 并将包添加到代码中..我的代码只有 HTML、CSS 和这个 JS 文件..
  • 你的标题说你不想用npm,你的帖子讲了一些和npm无关的东西,所以:为什么要提到npm?
  • 通常开发人员不使用 html 和 vuejs 并使用 jsx(也不确定)所以我不会在这方面吸引答案..
  • 这仍然与 npm 无关,但也不是真的。很多人使用 vue,很多其他人使用 react,但是更多的人使用“这两者都不是”。只需用正确的标签标记您的问题,并在必要的程度上对问题进行标题和描述,让其他人帮助您。

标签: javascript html css vue.js vuejs2


【解决方案1】:
<template>
  <div>
    <input v-model="text" @input="input" />
    <p id="x">
      Lorem ipsum dolor sit āmet, consetetur sadipscing elitr, sed diam nonumy
      eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
      voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
      clita kasd gubergren, nò sea takimata sanctus est Lorem ipsum dolor sit
      amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
      nonumy eirmod tempor invidunt ut labore et
    </p>
  </div>
</template>

<script>
/*eslint-disable */
export default {
  data() {
    return {
      t: String(""),
      text: String(""),
    };
  },
  mounted() {
    this.t = document.querySelector("#x").innerText;
  },
  methods: {
    input() {
      document.querySelector("#x").innerHTML = this.t.replaceAll(
        this.text,
        `<span class='h'>${this.text}</span>`
      );
    },
  },
};
</script>

<style>
.h {
  background: red;
}
</style>

简单的高亮搜索可能会对您有所帮助

【讨论】:

  • 实际上我有数据要单独显示,而不是一次全部显示在段落样式中,所以我可以添加参数进行搜索,以便可以搜索我的所有数据..like

    lorem ipsum...

    然后将“#x”的每个实例替换为“#x,#y”,以便在搜索时突出显示我的两个文本片段?
  • 您可以重新生成该代码。所以你需要添加标签(超过#x)并管理它们
  • 是的,所以基本上我有一个课程名称,然后有些还有子课程,然后还有作者......这需要结构良好以便显示......
  • 另外,如果您可以将代码转换为定义的 html、css 和 js 文件,那对我来说会更有用
猜你喜欢
  • 2019-02-19
  • 1970-01-01
  • 2012-05-21
  • 2021-07-24
  • 2011-04-13
  • 2014-09-23
  • 1970-01-01
  • 2021-09-03
  • 2022-12-28
相关资源
最近更新 更多