Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

135
Visualizações
How can I get h2 tags with vuejs?

There are h2 tags in the article. I'm aiming to use H2 tags as a table of contents.

Is there a way to get these tags because the H2 tags come within the article? I tried methods like Replace, substring, but I couldn't get only the h2 tags.

The post content comes as json like this

{
"link": "http://wordpress.example.com/2022/02/16/example/",
"title": { "rendered": "This is a test blog post" },
"content": {
   "rendered": "\n<h2><strong>This is a blog test thread</strong></h2>\n\n\n\n<p>This is a blog test content This is a blog test content This is a blog test content This is a blog test content This is a blog test content This is a blog test content</p>\n\n\n\n<h2><strong>this is a blog test thread 2</strong></h2>\n\n\n\n<p>This is a blog test content This is a blog test content....
}

 <div itemprop="articleBody" class="post-text" v-html="post.content.rendered" />

i get the data with v-html as post.content.rendered variable

html

This is a blog test thread

This is a blog test content This is a blog test content ....

As you can see, I couldn't get these tags because the h2 tags are embedded in the article. Is there a way to get the H2 tags?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can indeed achieve this via RegExp:

new Vue({
  el: '#app',
  data() {
    return {
      "link": "http://wordpress.example.com/2022/02/16/example/",
      "title": { "rendered": "This is a test blog post" },
      "content": {
         "rendered": "\n<h2><strong>This is a blog test thread</strong></h2>\n\n\n\n<p>This is a blog test content This is a blog test content This is a blog test content This is a blog test content This is a blog test content This is a blog test content</p>\n\n\n\n<h2><strong>this is a blog test thread 2</strong></h2>\n\n\n\n<p>This is a blog test content This is a blog test content...."
      }
    }
  },
  computed: {
    headlines() {
      return [ ...this.content.rendered.matchAll(/<h2>(.*?)<\/h2>/g) ].map(headline => headline[1])
    }
  }
})
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <ol v-for="headline of headlines">
    <li v-html="headline"></li>
  </ol>
  <div itemprop="articleBody" class="post-text" v-html="content.rendered" />
</div>

When you don't know, what the HTML contains, RegExp extraction might fail in some cases. Also using v-html with unsafe input isn't recommended.

It'd be better to actually parse the HTML:

new Vue({
  el: '#app',
  data() {
    return {
      "link": "http://wordpress.example.com/2022/02/16/example/",
      "title": { "rendered": "This is a test blog post" },
      "content": {
         "rendered": "\n<h2><strong>This is a blog test thread</strong></h2>\n\n\n\n<p>This is a blog test content This is a blog test content This is a blog test content This is a blog test content This is a blog test content This is a blog test content</p>\n\n\n\n<h2><strong>this is a blog test thread 2</strong></h2>\n\n\n\n<p>This is a blog test content This is a blog test content...."
      }
    }
  },
  computed: {
    headlines() {
      domParser = new DOMParser();
doc = domParser.parseFromString(this.content.rendered, "text/html");
      return [ ...doc.getElementsByTagName('h2') ].map(headline => headline.textContent )
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <ol v-for="headline of headlines">
    <li v-html="headline"></li>
  </ol>
  <div itemprop="articleBody" class="post-text" v-html="content.rendered" />
</div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda