在用户选择之外添加标签

时间:2011-12-01 08:38:08

标签: jquery selection

我正在尝试创建一个非常基本的所见即所得的编辑器。我经常搜索,但我没有成功地找到一些认为有效并回答我需要的东西。 我发现某人的代码应该在选择中添加标签,但它对我不起作用。你知道这个问题吗? 我需要确实知道是否有一些标签包装文本,如果是,我将能够更改它们,如果没有,我将添加一些标签。

非常感谢!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="rtl" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>check</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript">
$("p").live("mouseup",function() {
selection = getSelectedText();
if(selection.length >= 3) {
    var spn = '<span style="background-color: yellow;">' + selection +     '</span>';
    $(this).text($(this).html().replace(selection, spn));
}
});

//Grab selected text
function getSelectedText(){
if(window.getSelection){
    return window.getSelection().toString();
}
else if(document.getSelection){
    return document.getSelection();
}
else if(document.selection){
    return document.selection.createRange().text;
}
}
</script>
</head>
<body>
<p> check check check </p>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

这是一个可以帮助您构建自己的所见即所得编辑器的指南。 可能会在那里找到有用的东西: 链接Crete your own wysiwyg editor