如何向 p 标签添加一些类?

我想在 p 标签中添加一些类。我该怎么做?


const separatorsList = [

    { start: '<!-- Excerpt Start -->', end: '<!-- Excerpt End -->' },

    { start: '<p>', end: '</p>' }

  ];


慕尼黑5688855
浏览 177回答 2
2回答

慕的地8271018

您可以映射您的数组并将您的开始标签替换为包含您想要的类的标签。const separatorsList = [{&nbsp; &nbsp; start: '<!-- Excerpt Start -->',&nbsp; &nbsp; end: '<!-- Excerpt End -->'&nbsp; },&nbsp; {&nbsp; &nbsp; start: '<p>',&nbsp; &nbsp; end: '</p>'&nbsp; }];const res = separatorsList.map(({&nbsp; start,&nbsp; end}) => ({&nbsp; start: start.replace("<p>", "<p class='yourClass'>"),&nbsp; end}))console.log(res)

绝地无双

这是完整的代码,并提供一些背景知识。这是从eleventy.js。我的目标是为博客中的摘录设置样式。现在,我必须去每个 Markdown 文件并设置它的样式。我想知道有没有更好的方法,比如在 p 标签中添加一个全局变量。注意:对不起,我对javascript的了解很少。function extractExcerpt(article) {&nbsp; if (!article.hasOwnProperty('templateContent')) {&nbsp; &nbsp; console.warn('Failed to extract excerpt: Document has no property "templateContent".');&nbsp; &nbsp; return null;&nbsp; }&nbsp; let excerpt = null;&nbsp; const content = article.templateContent;&nbsp; // The start and end separators to try and match to extract the excerpt&nbsp; const separatorsList = [&nbsp; &nbsp; { start: '<!-- Excerpt Start -->', end: '<!-- Excerpt End -->' },&nbsp; &nbsp; { start: '<p class="f5 pt0 mt1 pl3 black-70">', end: '</p>' }&nbsp; ];&nbsp; separatorsList.some(separators => {&nbsp; &nbsp; const startPosition = content.indexOf(separators.start);&nbsp; &nbsp; const endPosition = content.lastIndexOf(separators.end);&nbsp; &nbsp; if (startPosition !== -1 && endPosition !== -1) {&nbsp; &nbsp; &nbsp; excerpt = content.substring(startPosition + separators.start.length, endPosition).trim();&nbsp; &nbsp; &nbsp; return true; // Exit out of array loop on first match&nbsp; &nbsp; }&nbsp; });&nbsp; return excerpt;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript