Jquery 如何在代码标记中的某些文本周围放置span标记?

Jquery 如何在代码标记中的某些文本周围放置span标记?,jquery,html,Jquery,Html,你好,谢谢你花时间来帮助我 这是我的页面设置: <code class="form-control" contenteditable="true" style="height: 100%; min-height: 100px"></code> <script src="~/Scripts/jquery-2.1.4.min.js"></script> <script src="~/Scripts/HashTagList.js"></

你好,谢谢你花时间来帮助我

这是我的页面设置:

<code class="form-control" contenteditable="true" style="height: 100%; min-height: 100px"></code>
<script src="~/Scripts/jquery-2.1.4.min.js"></script>
<script src="~/Scripts/HashTagList.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        HastTagLocation('form-control');

    });

function HastTagLocation(Controll) {
    var controller = $('.' + Controll);
    controller.keyup(function () {

    })
}
</script>

一个简单的解决方法是放置“!”在单词的开头,在结尾说“#”。然后插入以下代码(我自己没有测试过,但应该可以工作):

return$(this.text().replace(“!”,”);
返回$(this.text().replace(“#”,”);

你可以把它们结合在一起,但这是我能想到的最简单的解决方案。告诉我它是如何运行的。

您可以使用regex replace函数来执行此操作

// Get the code element.
var e = $('code');
// Get the text content.
var text = e.text();
// Replace the matches and wrap them with span tag.
var text = text.replace(/(\![a-zA-Z]*)/g, '<span class="mark">$1</span>');
// Set the html of the original element.
e.html(text);
//获取代码元素。
变量e=$(‘代码’);
//获取文本内容。
var text=e.text();
//替换匹配项并用span标记将其包装起来。
var text=text.replace(/(\![a-zA-Z]*)/g,$1');
//设置原始元素的html。
e、 html(文本);
您需要检查正则表达式并决定它是否适合您。就是它为
工作的时刻后跟0个或多个字母。如果需要其他字符,则需要调整正则表达式


也许值得尝试一下,它将用正则表达式替换文本内容。试一试,当你卡住时回来
应该
但这并不能完全提供解决方案。。见他的预期产出。。你还需要说他什么时候应该申请这个。。而且
#
不是他想要的。。他必须从
空间获得它
我同意,这只是一个非常简单的解决方法。有了您的建议,它应该为他提供正确的输出,否?添加
contenteditable
,以及一个事件处理程序,替换html会使光标跳转启动,以及一些其他问题。如果我这样做:我会遇到问题。每次我键入内容时,光标都会跳回起始位置。就像这样,我不知道这是问题所在,但有很多方法可以解决这个问题。例如,与预览窗格类似,仅在焦点丢失时应用更改。让它在键入时进行编辑(记住插入符号)并非不可能,但需要大量工作。我太想得到这样的答案了
// Get the code element.
var e = $('code');
// Get the text content.
var text = e.text();
// Replace the matches and wrap them with span tag.
var text = text.replace(/(\![a-zA-Z]*)/g, '<span class="mark">$1</span>');
// Set the html of the original element.
e.html(text);