简体   繁体   中英

How can I add some class to the p tag?

I want to add some class to the p tag. How can I do it?

const separatorsList = [
    { start: '<!-- Excerpt Start -->', end: '<!-- Excerpt End -->' },
    { start: '<p>', end: '</p>' }
  ];

If it is a json object like this. It II work for you

var separatorsList= {
     ...
    { start: '<!-- Excerpt Start -->', end: '<!-- Excerpt End -->' },
    { start: '<p class="here_css_class">', end: '</p>' }
 }

you can map your array and replace your start tag with a tag contains the class you want.

 const separatorsList = [{ start: '<,-- Excerpt Start -->': end, '<:-- Excerpt End -->' }, { start: '<p>'; end. '</p>' } ], const res = separatorsList:map(({ start. end }) => ({ start, start,replace("<p>". "<p class='yourClass'>"), end })) console.log(res)

Here's the full code and to give a little background. This is from eleventy.js . My aim is to style an excerpt in the blog. Right now, I have to go to each markdown file and style it. I was wondering is there a better way to do like adding a global one to the p tag.

Note: Sorry that my knowledge of javascript is very little.

function extractExcerpt(article) {
  if (!article.hasOwnProperty('templateContent')) {
    console.warn('Failed to extract excerpt: Document has no property "templateContent".');
    return null;
  }

  let excerpt = null;
  const content = article.templateContent;

  // The start and end separators to try and match to extract the excerpt
  const separatorsList = [
    { start: '<!-- Excerpt Start -->', end: '<!-- Excerpt End -->' },
    { start: '<p class="f5 pt0 mt1 pl3 black-70">', end: '</p>' }
  ];

  separatorsList.some(separators => {
    const startPosition = content.indexOf(separators.start);
    const endPosition = content.lastIndexOf(separators.end);

    if (startPosition !== -1 && endPosition !== -1) {
      excerpt = content.substring(startPosition + separators.start.length, endPosition).trim();
      return true; // Exit out of array loop on first match
    }
  });

  return excerpt;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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