嗨,我一直在使用contentEditable一段时间了,我觉得我对它有很好的处理.回避我的一件事是如何获得部分或完全在用户选择范围内的所有节点的引用数组.有人有个主意吗?
这是从一开始:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function getSelectedNodes(){
var sel = window.getSelection();
try{var frag=sel.getRangeAt(0).cloneContents()}catch(e){return(false);}
var tempspan = document.createElement("span");
tempspan.appendChild(frag);
var selnodes = Array() //<<- how do I fill this array??
var output = ''
for(i in selnodes){
output += "A "+selnodes[i].tagName+" was found\n"
//do something cool with each element here...
}
return(output)
}
</script>
</head>
<body contentEditable="true" onkeypress="return(keypress(event))">
<div>This <strong>div</strong> is <em>content</em> <span class='red'>editable</span> and has …Run Code Online (Sandbox Code Playgroud) 我对contenteditable元素的标记如下:
<iframe class="rich_text">
<html style="background:none transparent;">
<head></head>
<body contenteditable="true"></body>
</html>
</iframe>
Run Code Online (Sandbox Code Playgroud)
是否有身体的选择更改事件(contenteditable)?这样我就可以检测所选文本块是否有粗体/下划线等.
我尝试了一些事件处理程序(jQuery),但没有成功:
var richText = $(".rich_text"),
richTextDoc = richText.contents()[0],
richTextBody = richText.contents().find("body");
// Enable Design mode.
richTextDoc.open();
richTextDoc.write("");
richTextDoc.close();
richTextDoc.designMode = "on";
// Binds selection change event
$(richTextDoc).bind("select", function() { ... });
$(richTextDoc).bind("selectstart", function() { ... });
richTextBody .bind("select", function() { ... });
richTextBody .bind("selectstart", function() { ... });
Run Code Online (Sandbox Code Playgroud)