简体   繁体   English

从textarea更新最后一个目标contenteditable的html

[英]Update last targeted contenteditable's html from textarea

http://liveweave.com/1iWx1x http://liveweave.com/1iWx1x

My problem is simple. 我的问题很简单。

Select/focus on a few contenteditable elements. 选择/关注一些可编辑的元素。 Then select the textarea and change the last focused element's html from the textarea. 然后选择文本区域,并从文本区域更改最后一个焦点元素的html。 The problem is my textarea wants to update all the previously focused elements instead of the last one. 问题是我的textarea想要更新所有以前关注的元素,而不是最后一个。

If anyone can help it'd greatly appreciated. 如果有人可以帮助,将不胜感激。

$(document).ready(function() {
  // Grab selected element's html
  $("div").on('focus', function() {
    $("#show-selected-elms-code").val($(this).html());
    $("body").find("div").on('focus', function(e) {
      $("#show-selected-elms-code").val($(e.target).last().html());
      $(this).on('keyup change', function() {
        $("#show-selected-elms-code").val($(this).html());
      });
      $("#show-selected-elms-code").on('keyup change', function() {
        $(e.target).last().html($("#show-selected-elms-code").val());
      });
    });
  });

  $('#elm-font-family').on('click change', function() {
    document.execCommand('FontName',false,$(this).val());
  });
});

You can try this: 您可以尝试以下方法:

$(document).ready(function() {
  var selectedDiv;

  $('[contenteditable]').on('click',function(){
      selectedDiv=this;
  });

  $("div").on('focus keyup change', function() {
    $("#show-selected-elms-code").val($(this).html());
  });

  $("#show-selected-elms-code").on('keyup change', function() {
    $(selectedDiv).html($("#show-selected-elms-code").val());
  });

  $('#elm-font-family').on('click change', function(e) {
    document.execCommand('FontName',false,$(this).val());
  });
});

demo: http://liveweave.com/BfGir8 演示: http : //liveweave.com/BfGir8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM