javascript - contenteditable 在左键单击时不起作用

标签 javascript jquery html css jquery-ui

Contenteditable 不会专注于左键单击,而是专注于右键单击。为什么?我做错了什么。我认为是因为事件冒泡,如果是,那我该怎么办。

$(function() {
  $("#textTemplate").draggable({
    zIndex: 2500,
    helper: 'clone',
  });
  $("#editorDesignView").droppable({
    accept: '#textTemplate',
    drop: function(event, ui) {
      var html = '<div id="" style="background: #eee; width: 80%; margin: 10px auto; padding: 10px;"><p contenteditable="true" style="padding: 5px;">Add your text here.</p></div>';
      $(html).appendTo(this).hide().slideDown();
      var currentHtml = $("#editor").val();
      $("#editor").val(currentHtml + html);
    }
  });
  $('#editorDesignView').sortable();
});
* {
  box-sizing: border-box;
}

#wrapper {
  width: 100%;
  height: 610px;
}

#templateWrapper {
  width: 30%;
  height: 100%;
  float: left;
  overflow-y: scroll;
}

#editorBlock {
  width: 70%;
  height: 100%;
  float: left;
  position: relative;
  background-color: #f1f1f1
}

#editorDesignView {
  width: 100%;
  height: 100%;
}

#switch {
  background: #333;
  border: 1px solid #333;
  padding: 5px 20px;
  color: #fff;
  font-size: 1.5em;
  float: right;
  cursor: pointer;
}

#switch.active {
  background: #0091e1;
  border: 1px solid #0091e1;
}
<html>
<head>
  <meta charset="UTF-8">
  <title></title>
  <link rel="stylesheet" href="css/style.css">
</head>
<body>
  <div id="wrapper">
    <div id="templateWrapper">
      <div id="textTemplate" class="template">

        <div>Text</div>
      </div>
    </div>
    <div id="editorBlock">
      <div id="editorDesignView"></div>
    </div>
  </div>
  <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
  <script src='http://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js'></script>
  <script src="js/index.js"></script>
</body>
</html>

Here is my codepen link

最佳答案

如果一个元素同时是draggablecontenteditable编辑可能有一些问题。

添加 onclick='$(this).focus();' 使其工作

$(function() {
  $("#textTemplate").draggable({
    zIndex: 2500,
    helper: 'clone',
  });
  $("#editorDesignView").droppable({
    accept: '#textTemplate',
    drop: function(event, ui) {
      var html = '<div id="" style="background: #eee; width: 80%; margin: 10px auto; padding: 10px;"><p contenteditable="true" style="padding: 5px;" onclick="$(this).focus();">Add your text here.</p></div>';
      $(html).appendTo(this).hide().slideDown();
      var currentHtml = $("#editor").val();
      $("#editor").val(currentHtml + html);
    }
  });
  $('#editorDesignView').sortable();
});
* {
  box-sizing: border-box;
}

#wrapper {
  width: 100%;
  height: 610px;
}

#templateWrapper {
  width: 30%;
  height: 100%;
  float: left;
  overflow-y: scroll;
}

#editorBlock {
  width: 70%;
  height: 100%;
  float: left;
  position: relative;
  background-color: #f1f1f1
}

#editorDesignView {
  width: 100%;
  height: 100%;
}

#switch {
  background: #333;
  border: 1px solid #333;
  padding: 5px 20px;
  color: #fff;
  font-size: 1.5em;
  float: right;
  cursor: pointer;
}

#switch.active {
  background: #0091e1;
  border: 1px solid #0091e1;
}
<html>
<head>
  <meta charset="UTF-8">
  <title></title>
  <link rel="stylesheet" href="css/style.css">
</head>
<body>
  <div id="wrapper">
    <div id="templateWrapper">
      <div id="textTemplate" class="template">

        <div>Text</div>
      </div>
    </div>
    <div id="editorBlock">
      <div id="editorDesignView"></div>
    </div>
  </div>
  <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
  <script src='http://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js'></script>
  <script src="js/index.js"></script>
</body>
</html>

关于javascript - contenteditable 在左键单击时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43994412/

相关文章:

javascript - 使用 jQuery 一键单击多个可编辑字段

css - 菜单 div 的父 div 上的自动高度...如何?

javascript - 现场多次加载Angular.js的开销

javascript - 将新对象添加到现有对象数组 jquery/javascript

javascript - 单击模板中的按钮后如何运行 Django View 功能

jquery - 在没有 -UI 或插件的情况下使用 jQuery 淡化背景颜色的最简单方法是什么

javascript - 将数据从 Python 发送到 Javascript (JSON)

javascript - 更改帖子形式

javascript - 如何使用 AngularJS 获取子 HTML 元素属性值

html - 所有 float div 之间的底部边距相等