作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在句子中的随机位置呈现粗体字符串。
到目前为止,我一直在考虑这种方法
const wordToMatch = 'apples';
const sentence = 'I like apples and bananas';
sentence.split(' ').map(string=> {
return string === wordToMatch ? <b>{string}</b> : {string}
}).join(' ');
我收到了一组对象,其中一个(我匹配的那个)看起来像这样。
$$typeof: Symbol(react.element)
key: null
props: {children: undefined}
ref: null
type: "b"
_owner: FiberNode {tag: 0, key: null, elementType: ƒ, type: ƒ, stateNode: null, …}
_store: {validated: false}
最佳答案
这应该在不加入 map 结果的情况下运行良好,只需在不匹配的单词后添加空格,React render 将渲染 map 元素(字符串或 jsx 对象)
请看下面的片段:
const wordToMatch = 'apples';
const sentence = 'I like apples and bananas';
let output = sentence.split(' ').map(string=> {
return string === wordToMatch ? <b>{string} </b> : string+" "
});
ReactDOM.render(output,document.getElementById("txt"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="txt"></div>
关于javascript - 如何呈现句子中随机设置的粗体文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57673018/
下面是我用来制作 1px 文本描边轮廓的代码。但是如何使轮廓变粗呢?如果我只是用“5px”替换所有“1px”,结果看起来很疯狂。 HTML Hello! CSS .element { color:
我是一名优秀的程序员,十分优秀!