最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Find and select element in ckeditor - Stack Overflow

programmeradmin1浏览0评论

The following code snippet is returning an error in firebug:

Parameter is not an object" code: "1003
t.selectNode(s.$); ckeditor.js (line 11883)

My code is basically searching for elements of a certain type e.g input. I then want to make the current element of type selectElement as defined in the API here: .dom.selection.html#selectElement

var selectOption = dialog.getValueOf('find', 'findNext');
var documentWrapper = editor.document; // [object Object] ... CKEditor object
var documentNode = documentWrapper.$; // [object HTMLDocument] .... DOM object
elementArray = documentNode.getElementsByTagName(selectOption);
editor.getSelection().selectElement(elementArray[count]);   // Trying to make the     current element of type selectElement
var elementX = editor.getSelection().getSelectedElement();
alert('element ' + elementX.getName());

If I manually highlight an element in the WYSIWYG area then the last two lines of the above code snippet work, and getSelectedElement is defined in the same class as selectElement so I dont know why I'm getting the error.

The following code snippet is returning an error in firebug:

Parameter is not an object" code: "1003
t.selectNode(s.$); ckeditor.js (line 11883)

My code is basically searching for elements of a certain type e.g input. I then want to make the current element of type selectElement as defined in the API here: http://docs.cksource./ckeditor_api/symbols/CKEDITOR.dom.selection.html#selectElement

var selectOption = dialog.getValueOf('find', 'findNext');
var documentWrapper = editor.document; // [object Object] ... CKEditor object
var documentNode = documentWrapper.$; // [object HTMLDocument] .... DOM object
elementArray = documentNode.getElementsByTagName(selectOption);
editor.getSelection().selectElement(elementArray[count]);   // Trying to make the     current element of type selectElement
var elementX = editor.getSelection().getSelectedElement();
alert('element ' + elementX.getName());

If I manually highlight an element in the WYSIWYG area then the last two lines of the above code snippet work, and getSelectedElement is defined in the same class as selectElement so I dont know why I'm getting the error.

Share Improve this question edited May 10, 2013 at 14:12 codewaggle 4,9432 gold badges33 silver badges48 bronze badges asked Aug 10, 2011 at 2:54 oggiemcoggiemc 2732 gold badges4 silver badges12 bronze badges 2
  • I think you are using the wrong CKEditor method. What is your goal? To select elements? Or to select a single element by id? – Ken Commented Aug 10, 2011 at 3:06
  • No, i believe the selectElement() method is to highlight the element such that the getSelectedElement() method will return this highlighted element.. – oggiemc Commented Aug 10, 2011 at 14:33
Add a ment  | 

1 Answer 1

Reset to default 7

Some difficulties: getElementsByTagName creates a Node collection, not an array. The Node collection is very limited as far as available methods and properties are concerned.

Here is a concise explanation of the important things to know about Node collections.
A collection is not an array
http://www.sitepoint./a-collection-is-not-an-array/

After running getElementsByTagName, I moved the collection into an array. The elements were not in a usable format, so I also converted them into DOM elements.

Rather than working with an element selection, I used a range selection created from the element Node. I found ranges to be more flexible to work with.
http://docs.cksource./ckeditor_api/symbols/CKEDITOR.dom.range.html

Then at the end I created a DOM selection object containing the selected element. I created some sample objects using different methods that are available for a selection object.
http://docs.cksource./ckeditor_api/symbols/CKEDITOR.dom.selection.html

I saw your notes about the object types [object Object] and [object HTMLDocument]. Have you tried using " console.log(); " with FireBug? It shows you all the available methods and properties for each object. I added it for most of the objects in the included code. see what you think.

Look at the Console panel in FireBug to see the infomation about each object that log is run on. Try console.log( CKEDITOR ); to get a good overview of whats available.

Important Note: For Internet Explorer you need to open the Developer Tools window and activate Debugging in the Script panel while using " console.log(); ". Otherwise it will throw an error.

Here's the code:

var selectOption = dialog.getValueOf('find', 'findNext');
var documentWrapper = editor.document; // [object Object] ... CKEditor object
var documentNode = documentWrapper.$; // [object HTMLDocument] .... DOM object

// NEW - This isn't an array. getElementsByTagName creates a Node collection
// Changed name from elementArray to elementCollection
elementCollection = documentNode.getElementsByTagName(selectOption);

// NEW - Can't use array methods on Node Collection, so move into array and
// change the collection items into DOM elements
// NEW - Caveat: The collection is live,
// so if changes are made to the DOM it could modify the var elementCollection

var nodeArray = [];

for (var i = 0; i < elementCollection.length; ++i) {
    nodeArray[i] = new CKEDITOR.dom.element( elementCollection[ i ] );
}

// NEW - Working with an element object is problematic.
// Create a range object to use instead of an element
// http://docs.cksource./ckeditor_api/symbols/CKEDITOR.dom.range.html
var rangeObjForSelection = new CKEDITOR.dom.range( editor.document );
console.log(rangeObjForSelection);

// NEW - Populate the range object with the desired element
rangeObjForSelection.selectNodeContents( nodeArray[ count ] );
console.log(rangeObjForSelection);

// OLD - editor.getSelection().selectElement(elementCollection[count]);
// Trying to make the current element of type selectElement
// NEW - Highlight the desired element by selecting it as a range
editor.getSelection().selectRanges( [ rangeObjForSelection ] );

// OLD - var elementX = editor.getSelection().getSelectedElement();
// NEW - Create a DOM selection object.
var selectedRangeObj = new CKEDITOR.dom.selection( editor.document );
console.log(selectedRangeObj);

// NEW - You can look at the properties and methods available
// http://docs.cksource./ckeditor_api/symbols/CKEDITOR.dom.selection.html
// I've created sample objects using the methods that seem most useful.

var elementX = selectedRangeObj.getRanges();
console.log(elementX);

var elementX2 = selectedRangeObj.getStartElement();
console.log(elementX2);

var elementX3 = selectedRangeObj.getSelectedText();
console.log(elementX3);

var elementX4 = selectedRangeObj.getNative();
console.log(elementX4);

Be Well, Joe

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>