代码之家  ›  专栏  ›  技术社区  ›  Samuil Banti

jquery获取cut的值

  •  0
  • Samuil Banti  · 技术社区  · 8 年前

    正如您在片段中看到的,我可以得到 粘贴到输入中的字符串。 我想知道是否有一个简单的方法来获得切割后的字符串的值。

    	    $('#example').on("paste cut", function(e) {
              var value = '';
              if(e.type == 'paste') {
                  value = e.originalEvent.clipboardData.getData('text');
              } else if(e.type == 'cut') {
                  // How should I get the removed part of the string
              }
              $('#result').html(value);
    	    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
    <input type="text" id="example" value="Some random value" style="width:80%"></div>
    <div id="result"></div>
    1 回复  |  直到 8 年前
        1
  •  2
  •   Carsten Massmann    8 年前

    window.getSelection().toString()

    	    $('#example').on("paste cut", function(e) {
              var value = e.type=='paste'
                        ? e.originalEvent.clipboardData.getData('text')
                        : window.getSelection().toString();
              console.log(e.type+': '+value);
              $('#result').html(value);
    	    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
    <input type="text" id="example" value="Some random value" style="width:80%"></div>
    <div id="result"></div>