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

时间:2015-03-26 10:16:04

标签: jquery dom ckeditor

如何使用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或其他任何内容中选择span ID = sub3,然后获取其内容(此处:{{1} }}及其属性Name(此处:data-time-start)?

此外,在循环中,如何选择具有类6&gt;

的每个元素

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

sub

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

$.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 ??    
});

2 个答案:

答案 0 :(得分:1)

我在我的桌面上克隆你的项目,它对我有用!但在小提琴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的回调,所以输入超时有点延迟,为此,我测试它 - 一切正常!

答案 1 :(得分:0)

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

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

- 谢谢你。