我想知道,为什么我不能在一个元素上调用CKEDITOR的replace()函数,但是如果我给该元素一个ID,那么我可以调用replace引用那个ID吗? 这是我的功能(ele来自onclick=myFunction(this) )
myFunction(ele){ if(~$(ele).closest("ul").attr("class").indexOf("sh_connect")){ var _sh = $(ele).closest("li").find("[name='shortcode[]']"); _sh.attr("id","curr_edit"); //CKEDITOR.replace(_sh) //doesn't work CKEDITOR.replace('curr_edit'); //works console.log(_sh); //prints [textarea#curr_edit.form-control, prevObject: r.fn.init(1)] } }如何调用replace()引用元素?
I'm wondering, why I can't call the replace() function from CKEDITOR on an element, but I can if I give that element an ID, then call replace referencing that ID? This is my function (ele is from onclick=myFunction(this) )
myFunction(ele){ if(~$(ele).closest("ul").attr("class").indexOf("sh_connect")){ var _sh = $(ele).closest("li").find("[name='shortcode[]']"); _sh.attr("id","curr_edit"); //CKEDITOR.replace(_sh) //doesn't work CKEDITOR.replace('curr_edit'); //works console.log(_sh); //prints [textarea#curr_edit.form-control, prevObject: r.fn.init(1)] } }How can I call replace() referencing just the element?
最满意答案
var _sh = $(ele).closest("li").find("[name='shortcode[]']"); 给你一个jQuery对象。 您需要迭代它以访问每个DOM元素并调用replace()如下所示:
_sh.each(function() { CKEDITOR.replace(this); });或者如果你知道find()只返回一个元素,你可以像这样使用DOM元素:
CKEDITOR.replace(_sh.get(0));要么
CKEDITOR.replace(_sh[0]);更多信息:
。每()
.each()方法旨在使DOM循环结构简洁且不易出错。 调用时,它会迭代属于jQuery对象的DOM元素。 每次回调运行时,都会从0开始传递当前循环迭代。更重要的是,回调是在当前DOM元素的上下文中触发的,因此关键字this指的是元素。
。得到()
.get()方法授予对每个jQuery对象下面的DOM节点的访问权限。 指定索引后, .get( index )检索单个元素。
console.log( $( "li" ).get( 0 ) );
每个jQuery对象也伪装成一个数组,因此我们可以使用数组解引用运算符来获取列表项:
console.log( $( "li" )[ 0 ] );
var _sh = $(ele).closest("li").find("[name='shortcode[]']"); gives you a jQuery object. You need to iterate over it to access each DOM element and call replace() like this:
_sh.each(function() { CKEDITOR.replace(this); });Or if you know that find() will return only one element, you can use the DOM element like this:
CKEDITOR.replace(_sh.get(0));or
CKEDITOR.replace(_sh[0]);More info:
.each()
The .each() method is designed to make DOM looping constructs concise and less error-prone. When called it iterates over the DOM elements that are part of the jQuery object. Each time the callback runs, it is passed the current loop iteration, beginning from 0. More importantly, the callback is fired in the context of the current DOM element, so the keyword this refers to the element.
.get()
The .get() method grants access to the DOM nodes underlying each jQuery object. With an index specified, .get( index ) retrieves a single element.
console.log( $( "li" ).get( 0 ) );
Each jQuery object also masquerades as an array, so we can use the array dereferencing operator to get at the list item instead:
console.log( $( "li" )[ 0 ] );
更多推荐
发布评论