Onenter按删除innerHTML

时间:2017-01-19 05:05:21

标签: javascript jquery html

我有

<p contenteditable="true" data-textchanged="false">
  <button type="button" data-bind=" jqueryui: 'button', click: goto_step.bind($data, 'NCS2')">click for answer</button>
</p>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p contenteditable="true" data-textchanged="false">
  <button type="button" data-bind=" jqueryui: 'button', click: goto_step.bind($data, 'NCS2')">click for answer</button>
</p>

但是当我在p标签中按Enter键时,它会显示按钮,代码就像

    <p contenteditable="true" data-textchanged="false">
        <button type="button" data-bind=" jqueryui: 'button', click: goto_step.bind($data, 'NCS2')">Show script</button><p><button type="button" data-bind=" jqueryui: 'button', click: goto_step.bind($data, 'NCS2')"><br></button></p>
</p>

但我需要的是

<p contenteditable="true" data-textchanged="false">
    <button type="button" data-bind=" jqueryui: 'button', click: goto_step.bind($data, 'NCS2')">Show script</button><p></p></p>

(这里我显示按钮但它可能是任何标签) 只有单个p标签不在其中的按钮..我只想要输入空白innerHTML的p标签按

1 个答案:

答案 0 :(得分:0)

你好,当你点击按钮时检查这段代码,从p标签中删除了innerHtml,id是contentToRemove。你可以查看实时查看链接链接如下:

https://jsfiddle.net/fatehjagdeo/tdkj77aa/

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <p >
      <button type="button" class="button" >click for answer</button>
    </p>
    <p id="contentToRemove">
    anything in this p tag
    </p>

    <script>
    $(document).on('click','.button',function(){
    $('#contentToRemove').html('');
    });
    </script>