【发布时间】:2019-12-31 19:43:14
【问题描述】:
我正在尝试做一个强调特定单词的 chrome 扩展。 假设用户写“你好”,那么我想给它加下划线。 在 Facebook 和其他社交媒体中,文本字段是内容可编辑的 div。
如何访问该文本并仅操作其中的特定单词? 我已经尝试过这样做,但没有成功
$('body').on('focus', '[contenteditable]', function() {
const $this = $(this);
$this.data('before', $this.html());
}).on('blur keyup paste input', '[contenteditable]', function() {
const $this = $(this);
if ($this.data('before') !== $this.html()) {
$this.data('before', $this.html());
$this.trigger('change');
if ($this.text().includes("hello")){
//document.execCommand('undeline'); //didn't work
//$this.style.textDecorationColor="red"; // neither did this
//$this.style.textDecorationStyle="wavy";
}
}
});
注意:我试过document.execCommand,但没有给出任何结果
【问题讨论】:
-
检查 this answer 它可能会帮助您进行造型
标签: javascript jquery google-chrome-extension