首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我需要找到一个字符,并用HTML标记包装它后面的单词

我需要找到一个字符,并用HTML标记包装它后面的单词
EN

Stack Overflow用户
提问于 2014-09-27 00:11:06
回答 4查看 94关注 0票数 0

在我用Jekyll生成的列表中,我需要用强标签包装某些单词。我的想法是使用分隔符

代码语言:javascript
复制
<li>100g of |sugar</li>

会变成

代码语言:javascript
复制
<li>100g of <strong>sugar<strong></li>

该列表由YAML中的数组生成,

代码语言:javascript
复制
ingredients:
  -name: 100 grams of |sugar
EN

回答 4

Stack Overflow用户

发布于 2014-09-27 00:18:57

这里的问题是强标记没有闭合,所以您可以在列表中使用类似以下内容:

代码语言:javascript
复制
ingredients:
     -name: 100 grams of %strongo%sugar%strongc%

然后在jquery脚本中使用以下代码:

代码语言:javascript
复制
str = str.replace("%strongo%","<strong>");
str = str.replace("%strongc%","</strong>");

然后它将被转换为:

代码语言:javascript
复制
<li>100g of <strong>sugar</strong></li>
票数 0
EN

Stack Overflow用户

发布于 2014-09-27 04:50:13

您可以简单地在前面的内容中使用markdown:

代码语言:javascript
复制
---
layout: page
title: recipe

ingredients:
  - 100 grams of **sugar**
  - 200 grams of **floor**
---

<ul>
{%for ingredient in page.ingredients %}
    <li>{{ingredient | markdownify}}</li>
{% endfor %}
</ul>

完成了!

票数 0
EN

Stack Overflow用户

发布于 2014-09-27 07:53:18

给定以下HTML:

代码语言:javascript
复制
<ul class="ingredients">
    <li>100g of |sugar</li>
    <li>50ml of |creme fraiche</li>
    <li>200g of |strong plain flour</li>
    <li>3 |free range eggs</li>
    <li>2 |turtle doves</li>
    <li>1 |partridge, in a |pear tree</li>
</ul>

以下jQuery似乎可以正常工作:

代码语言:javascript
复制
// iterates over the 'li' elements within the element(s) of class 'ingredients',
// uses the anonymous function of the html() method,
// the first argument ('i') is the index of the current element in the collection,
// the second ('h') is the found html:
$('.ingredients li').html(function(i,h){
    // we call replace() on the found-html,
    // finding the | character (escaped because in a regex it's an OR special character),
    // followed by a sequence of one, or more, alphanumerics, until a character
    // is found from the second set (the ^ is the NOT operator, within character-classes:
    return h.replace(/\|([\w][^|,.:?()_]+)/g,'<strong>$1</strong>');
});

代码语言:javascript
复制
$('.ingredients li').html(function(i, h) {
  return h.replace(/\|([\w][^|,.:?()_]*)/g, '<strong>$1</strong>');
});
代码语言:javascript
复制
.ingredients {
  margin: 0;
  padding: 0;
}
li {
  list-style-type: none;
  padding-left: 0.5em;
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="ingredients">
  <li>100g of |sugar</li>
  <li>50ml of |creme fraiche</li>
  <li>200g of |strong plain flour</li>
  <li>3 |free range eggs</li>
  <li>2 |turtle doves</li>
  <li>1 |partridge, in a |pear tree</li>
</ul>

参考文献:

  • JavaScript

  • html().

  • jQuery

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26063859

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档