can';使用javascript无法访问DIV的节点内容
当使文本DIV可编辑时,我使用setEndOfContenteditable函数将光标放在文本的末尾can';使用javascript无法访问DIV的节点内容,javascript,jquery,html,jquery-selectors,contenteditable,Javascript,Jquery,Html,Jquery Selectors,Contenteditable,当使文本DIV可编辑时,我使用setEndOfContenteditable函数将光标放在文本的末尾 //Insert cursor at end of contentEditable element function setEndOfContenteditable(contentEditableElement) { var range,selection; if(document.createRange)//Firefox, Chrome, Opera, Safari, IE
//Insert cursor at end of contentEditable element
function setEndOfContenteditable(contentEditableElement)
{
var range,selection;
if(document.createRange)//Firefox, Chrome, Opera, Safari, IE 9+
{
range = document.createRange();//Create a range (a range is a like the selection but invisible)
range.selectNodeContents(contentEditableElement);//Select the entire contents of the element with the range
range.collapse(false);//collapse the range to the end point. false means collapse to end rather than the start
selection = window.getSelection();//get the selection object (allows you to change selection)
selection.removeAllRanges();//remove any selections already made
selection.addRange(range);//make the range you have just created the visible selection
}
else if(document.selection)//IE 8 and lower
{
range = document.body.createTextRange();//Create a range (a range is a like the selection but invisible)
range.moveToElementText(contentEditableElement);//Select the entire contents of the element with the range
range.collapse(false);//collapse the range to the end point. false means collapse to end rather than the start
range.select();//Select the range (make it the visible selection
}
}
当我以前只有一个div时,其中的文本将非常适合:
setEndOfContenteditable(curObj);
curObj是包含文本的DIV
现在我已经切换到一种设计,在这种设计中,curObj-div仍然是一个框架,而包含实际文本的div位于框架内部。通常我这样做是为了到达(选择)jQuery代码中的内部div
var curObj = window.curObj;
var inner = '#' + $(curObj).attr("id") + ' .object_inner';
$(inner).doAllMyStuff;
但是,这不适用于这个特定的setEndOfContenteditable函数,我得到的错误是
Uncaught Error: NotFoundError: DOM Exception 8
以及setEndOfContenteditable函数第7行的错误点:
range.selectNodeContents(contentEditableElement);//Select the entire contents of the element with the range
啊,我试图将jQuery选择器传递给一个普通javascript函数(该函数需要一个原始DOM元素)。下面是一个解决方案,它获取原始DOM元素:
var spec = $(curObj).find('.object_inner');
setEndOfContenteditable(spec[0]);
特别感谢这家伙: