在每次点击div时,在文本区域中获取动态文本

时间:2012-11-19 13:34:07

标签: javascript jquery html

我想在CKEDITOR的{​​{1}}上打开click-event,并希望在div的ckeditor中使用div内容 但不知何故,这是行不通的。

提前致谢,对不起我的英语不好

textarea

4 个答案:

答案 0 :(得分:0)

删除createEditor()函数并尝试

 $('DIV').click(function(event) {
    var id1 = event.target.id;
    //alert(id1);

    document.getElementById("editor1").value = '';
    var newtext = document.getElementById(id1).innerHTML;
    alert(newtext);
    document.getElementById("editor1").value += newtext;
});

document.getElementById("contents").style.display = "block";

答案 1 :(得分:0)

尝试将此数据添加到textarea。

$("#editor1").val(newtext);

或使用

$('#editor1').append(newtext); 

答案 2 :(得分:0)

此解决方案将更多地依赖于jQuery。首先,如果您希望在单击div时触发此功能,请删除function(),这是没有必要的,因为每次点击div时都会触发您的事件。

这可以解决你的问题:

$('div').click(function(e) {
    $id1 = $(this).attr("id");
    alert($id1);

   $("#editor1").val(' '); //Are you sure you want to empty your textarea?
   $newtext = $("#"+$id1).html();
   alert($newtext);
   $("#editor1").val($newtext); //Because when you append the newtext it won't append but replace the text it's already on this.

   $("#contents").css("display","block");
});

这是fiddle,其中包含使用您的函数的类似示例:

答案 3 :(得分:0)

如果您只想要点击div的文本,并且您正在使用jQuery,请尝试

$("div").click( function() {
    var newtext = $(this).text();
    $("#editor1").text( newtext );
});