【问题标题】:How do you add a class to an appended element that is created when contenteditable adds new paragraph tag when pressing enter?当 contenteditable 在按下回车键时添加新的段落标签时,如何将类添加到创建的附加元素?
【发布时间】:2017-09-17 19:38:59
【问题描述】:

我正在使用 Hashedit (contenteditable),当我在可编辑的文本正文(段落元素)上按 Enter 键时,它会附加一个新的段落元素。但是,我希望新的段落元素也具有与我按 Enter 键相同的类。

【问题讨论】:

    标签: javascript jquery mutation-observers


    【解决方案1】:

    function init() {
      $("#my-div").on("keydown",function(e) {
        if (e.keyCode==13) {
          //Do your thing when enter is pressed
          setTimeout('$("p:last").addClass("cool-class")',10);
        }
      });
    }
    .cool-class {
      background-color: #f00;
      color: #fff;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <body onload="init()">
      <div contentEditable id="my-div">
        <p>Bla Bla Bla</p>
        <p>Bla bla</p>
      </div>
    </body>

    使用 jQuery。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多