用户
 找回密码
 入住 CI 中国社区
搜索
查看: 6923|回复: 5
收起左侧

[讨论/交流] 用了KindEditor无法获取ID

[复制链接]
发表于 2011-11-4 23:36:24 | 显示全部楼层 |阅读模式
本帖最后由 haoren26 于 2011-11-5 00:07 编辑

CI version:2.0.3

KindEditor version:4.0.2

Environment:WAMP2.1      Windows2003

我写了一个文章页面,下部是评论框,很普通的页面(见图1)

本来点击[REPLY]链接可以跳到锚点#submit,大致位于"1 comments"位置,然后调用JS函数将textarea选中,给予焦点,添加"Re_author"字样,给它的属性赋值等等。。。

public.js
JS复制代码
 
// focus submit_box and append 'Re_author:'
function reply_focus(){
                                        setTimeout( function(obj){ try{
                                        d = document.getElementById('submit_box');
                                        d.focus();
                                        d.select();
                                        record_author = obj.title;
                                        d.value="Re_"+record_author+":";
                                        } catch(e){}
                                        },1);
                                        }
 
复制代码


comment_view.php (仅显示部分代码)
PHP复制代码
 
<!--        comment begin     -->
<?=anchor('blog/comment/'.$art_row->id.'#submit','[REPLY]',array('onclick'=>'reply_focus(this);',
         'id'=>$com_row->id,'title'=>$com_row->comment_author))?>
<!--        comment end       -->
 
<!--        submit_comment begin         -->
<script charset="utf-8" src="editor/kindeditor.js"></script>
<script>
var editor;
var options = {
items : [ 'emoticons','link'],
filterMode : true
};
KindEditor.ready(function(K) {
editor = K.create('#submit_box',options);
});
  </script>
  <div id="comment_submit">
  <a name="submit"></a>
  <p><textarea id="submit_box" rows="10" cols="50" name="comment_content"></textarea></p> <!--改造前 -->
 
  </div>
 
<!--       submit_comment end                -->
 
复制代码


现在的问题是:我要把简单textarea改造成带编辑器的文本框,按照官网的方法成功实现了改造,但是。。。整个的结构改变了,textarea没了,它的ID也没了!!而JS就是通过ID对textarea进行操作的。我现在点击[REPLY], 无法自动选中评论文本框和添加"Re_author"字样

所以我研究了下KE编辑器的JS代码,没找到可以添加ID的地方,以前的版本好像有初始化参数"id",但我用的4.0.2没有。所以我就纳闷了,怎么给编辑器的某个地方(最接近文字的标签是<body>)添加ID?

有点杂乱,不知道各位看懂我说的了么?

图1:
未命名.jpg



发表于 2011-11-6 18:38:46 | 显示全部楼层
KE的文档

JS复制代码
 
// focus submit_box and append 'Re_author:'
function reply_focus(){
                                        setTimeout( function(obj){ try{
                                        d = document.getElementById('submit_box');
                                        d.focus();
                                        d.select();
                                        record_author = obj.title;
                                        d.value="Re_"+record_author+":";
                                        } catch(e){}
                                        },1);
                                        }
 
复制代码

你的js脚本中的d要换成ke的instance,然后设置d的内容用文档中的方法。我只提供一个思路,如果还是不行的话,抱歉我帮不到更多。
 楼主| 发表于 2011-11-6 23:20:37 | 显示全部楼层
huboo82 发表于 2011-11-6 18:38
KE的文档

困惑:KindEditor.ready(function(K) {
editor = K.create('#submit_box',options);
}); 这个方法返回了一个KEditor的实例赋给了editor,但这个实例是在函数function(K)内的(虽然是在函数外定义的),属于局部变量,我在这个函数外就无法使用该实例的任何方法。该怎么才能把它传出来呢?在editor = K.create('#submit_box',options);之后写一个函数把editor作为参数传进去,然后。。。要是能把它变成全局变量就好了
发表于 2011-11-7 00:02:05 | 显示全部楼层
本帖最后由 huboo82 于 2011-11-7 00:04 编辑

comment_view.php
JS复制代码
 
<script charset="utf-8" src="editor/kindeditor.js"></script>
<script>
var editor;
var options = {
items : [ 'emoticons','link'],
filterMode : true
};
KindEditor.ready(function(K) {
editor = K.create('#submit_box',options);
});
</script>
 
复制代码

这个editor就是全局的。

 楼主| 发表于 2011-11-8 13:28:09 | 显示全部楼层
本帖最后由 haoren26 于 2011-11-8 13:53 编辑
huboo82 发表于 2011-11-7 00:02
comment_view.php

这个editor就是全局的。

JS复制代码
 
<script charset="utf-8" src="editor/kindeditor.js"></script>
<script>
var editor;
var options = {
items : [ 'emoticons','link'],
filterMode : true
};
KindEditor.ready(function(K) {
editor = K.create('#submit_box',options);
editor.html('test'); //如果加在此处,编辑框内有显示"test"(没有引号)
});
editor.html('test'); //如果加在此处,编辑器内就什么都没有
</script>
 
复制代码


还有,我在<head>里写的JS代码也调用不了editor这个实例。我甚至把var editor;这个声明语句删除了,以防editor成为局部变量
 楼主| 发表于 2011-11-8 14:44:22 | 显示全部楼层
haoren26 发表于 2011-11-8 13:28
还有,我在里写的JS代码也调用不了editor这个实例。我甚至把var editor;这个声明语句删除了,以防edito ...

我解决了,可能跟浏览器加载JS代码有关吧?我把代码顺序换了下就调用成功了,editor确实是全局变量。
谢谢了!

本版积分规则