CKEDITOR replace()引用元素不起作用(CKEDITOR replace() referencing element doesn't work)

系统教程 行业动态 更新时间:2024-06-14 16:57:18
CKEDITOR replace()引用元素不起作用(CKEDITOR replace() referencing element doesn't work)

我想知道,为什么我不能在一个元素上调用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 ] );

更多推荐

本文发布于:2023-04-12 20:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/cc46d78c2a564aeae60af381a758bd87.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   元素   replace   CKEDITOR   element

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!