CKEDITOR - 按ID或CLASS选择span并获取其数据属性值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CKEDITOR - 按ID或CLASS选择span并获取其数据属性值相关的知识,希望对你有一定的参考价值。

如何使用jQuery管理CKEditor中的内容?

我有这个html

<div id="ckeditor_block">
   <textarea id="editor1">
      <span class="sub" id="sub1" data-time-start="0">Hello </span>
      <span class="sub" id="sub2" data-time-start="2">My </span>
      <span class="sub" id="sub3" data-time-start="6">Name </span>
      <span class="sub" id="sub4" data-time-start="8">Is </span>
      <span class="sub" id="sub5" data-time-start="12">Zoob</span>
   </textarea>                
</div>

我的JS:

var textarea;

$(document).ready(function () {
    textarea = $('#ckeditor_block').find('textarea').attr('id');
    ckeditor_init();
});

function ckeditor_init() {
    CKEDITOR.replace(textarea, {
        language: 'fr',
        allowedContent: true
    });
}

JSFIDDLE

在此之前,这很好用。

现在我怎么能简单地在CKeditor iframe或其他什么中选择spanID = sub3,之后得到它的内容(这里:Name)和它的属性data-time-start(这里:6)?

此外,在循环中,如何使用类sub>选择每个元素

像这样的东西,但用CKEDITOR的方法:

$.each($(CKEditor.getBody()).find(".sub"), function () { // How select SUB class name in CKEDITOR ??
     var d = $(this).attr("data-time-start"); // HOW SELECT FOR EACH 'SUB' HIS data-time-sart attribute in CKEDITOR ??
     $(".st").removeClass("active"); // HOW REMOVE CLASS FOR ALL class 'SUB' in CKEDITOR ??
     $(this).addClass("active"); // HOW ADD CLASS 'active' TO THIS 'SUB' in CKEDITOR ??    
});

我试图获得这样的内容,但之后。我该怎么做呢?

 var editor = CKEDITOR.instances[textarea];
 var $dataCkeditor = editor.getData();
答案

我在我的桌面上克隆你的项目,它对我有用!但在小提琴IT不起作用!小心!我不知道为什么,但在小提琴它只是不工作,试试:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="http://ckeditor.com/apps/ckeditor/4.0/ckeditor.js"></script>
<script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.0/js/bootstrap.min.js"></script>
</head>
<body>
<div id="ckeditor_block">
    <textarea id="editor1"> <span class="sub" id="sub1" data-time-start="0">Hello </span>
     <span class="sub" id="sub2" data-time-start="2">My </span>
     <span class="sub" id="sub3" data-time-start="6">Name </span>
     <span class="sub" id="sub4" data-time-start="8">Is </span>
     <span class="sub" id="sub5" data-time-start="12">Zoob</span>
    </textarea>
</div>
<script type='text/javascript'>
var textarea;
$(document).ready(function () {
    textarea = $('#ckeditor_block').find('textarea').attr('id');
    CKEDITOR.replace(textarea, {
        language: 'fr',
        uiColor: '#9AB8F3',
        allowedContent: true,
        disallowedContent: 'script; *[on*]',
        enterMode: CKEDITOR.ENTER_BR,
        toolbar: [
            ['Bold', 'Italic', 'Underline', '-', 'TextColor', '-', 'RemoveFormat'],
            ['Cut', 'Copy', 'Paste', '-', 'Undo', 'Redo'],
            ['JustifyLeft', 'JustifyCenter', 'JustifyRight'],
            ['Find', 'Replace', 'SelectAll'],
            ['Source', '-', 'Maximize', '-', 'Save']
        ]
    });
    function waitInit(){
        var subs = $( CKEDITOR.instances.editor1.window.getFrame().$ ).contents().find('.sub' );
        $.each(subs, function(){
            $this = $(this);
            console.log("value = " + $this.text());
            console.log("time = " + $this.attr('data-time-start'));
        });
    }
    CKEDITOR.on('instanceReady', function(){ waitInit(); });
});   
</script>
</body></html>

这是2个麻烦 - 首先是小提琴,第二个 - 我们在ckeditor初始化之前调用函数,我无法找到init的回调,所以输入超时有点延迟,为此,我测试它 - 一切正常!

另一答案

或者您只是做了以下事情,

var ele = $(editor.editable().$);
$('span',ele).each(function(){
   console.log($(this).text());
   console.log($(this).attr('id'));
});
//editor represent your instance in CKEditor

-谢谢。

以上是关于CKEDITOR - 按ID或CLASS选择span并获取其数据属性值的主要内容,如果未能解决你的问题,请参考以下文章

CKEditor不在laravel请求中工作

如何在Ckeditor编辑器中选择中文字体?

无法使 CKEditor InsertHTML 在 IE 8 上工作

Ckeditor按钮ID正在通过浏览器进行更改

jQuery 中的 DOM 选择器,用于没有 ID 或 CLASS 名称标签的元素

javascript向ckeditor5中插入附件