javascript - 生成花哨/替代文本
问题描述
我很好奇如何构建一个允许您输入文本并使用 javascript 以不同样式动态显示它的网页。
很像:(ρ૨εѵเεω ƭεאƭ
来源)
编辑:找到这个(所以问题)
function Demo() {
const style1 = {
'a' : '',
'b' : '',
'c' : '',
'd' : '',
};
const style2 = {
'a' : '',
'b' : '',
'c' : '',
'd' : '',
};
function strtr(s, p, r) {
return !!s && {
2: function () {
for (var i in p) {
s = strtr(s, i, p[i]);
}
return s;
},
3: function () {
return s.replace(RegExp(p, 'g'), r);
},
0: function () {
return;
}
}[arguments.length]();
}
const input = document.querySelector('#input');
const style = style1; // Text style
let outputt = strtr(input, style);
console.log(outputt);
return (
<div>
<Form>
<Form.Item>
<Input id="input" placeholder="input text" />
</Form.Item>
</Form>
</div>
<div>
<p>Style One:</p>
<Form>
<Form.Item>
<Input size="large" id="output"/>
</Form.Item>
</Form>
</div>
);
}
export default Demo;
但是我的输出没有呈现在表单中。还是取得了一些进展
解决方案
假设您有一组具有不同样式的字母(在任何编程语言中,我们将使用 Javascript)。
样式需要扩展并添加更多......,我只是试图给你一个例子。
const style1 = {
'a' : '',
'b' : '',
'c' : '',
'd' : '',
};
const style2 = {
'a' : '',
'b' : '',
'c' : '',
'd' : '',
};
function strtr(s, p, r) {
return !!s && {
2: function () {
for (var i in p) {
s = strtr(s, i, p[i]);
}
return s;
},
3: function () {
return s.replace(RegExp(p, 'g'), r);
},
0: function () {
return;
}
}[arguments.length]();
}
const str = 'abc'; // Text
const style = style1; // Text style
let output = strtr(str, style);
console.log(output);
更新
在contructor()
方法的类中,我们需要定义我们的状态:
constructor(props) {
super(props);
this.state = {output: ""};
}
同样在 JSX 中:
<p>Style One:</p>
<div>{this.state.output}</div>
最后在addEventListener
's 回调中:
this.setState({output: String.fromCharCode(55349, code + 56658)});
推荐阅读
- javascript - Reactstrap 工具提示被放置在整个 div 的底部,而不是它的子元素
- mysql - 如何通过 Node Js 模块文件运行 MySQL 查询
- c# - 为什么不能区域测试点的范围?
- javascript - 如何在 React with Typescript 中传递具有所需道具的组件数组?
- python - 如何使用 for 循环打印乘法表?
- node.js - 节点 RabbitMQ 消费消息并为每条消息做一些事情
- mysql - mysql group_concat 和 group by 不工作
- javascript - 数学函数总是显示 0 或根本不工作
- c++ - 有没有办法使用 libxml2 自动检测根标签中写入的命名空间?
- java - 我如何在没有存储库的情况下实现分页休息弹簧