Ace代码编辑器在onclick上消失

时间:2016-04-16 19:47:19

标签: javascript html web polymer-1.0 ace-editor

我在页面中嵌入了ace-widget。我添加了一个使用getvalue()方法读取编辑器会话值的按钮。单击按钮后编辑器消失。可能是错误的原因?请在下面的链接中找到截图。

<ace-widget id="editor1" initial-focus theme="ace/theme/monokai" mode="ace/mode/json" softtabs="true" wrap="true">
            This is a nice widget... and we are writing a long text here to show the effects of the `wrap` attribute.
          </ace-widget>       
          <paper-button raised class="custom indigo" onclick="myrun()">Run</paper-button>             
          <paper-textarea label="autoresizing textarea input"></paper-textarea>
          <script type="text/javascript">
            function myrun(){
              var editor = ace.edit("editor1");
              var myVar = editor.getSession().getValue();
              myVar= myVar.replace(/\d+/, '');
              myVar = myVar.substring(0,myVar.indexOf('XXXXXXX'));
              alert(myVar);
            }
          </script>

Screenshot

1 个答案:

答案 0 :(得分:0)

您需要使用document.getElementById("editor1").editor代替ace.edit("editor1")

&#13;
&#13;
<script src="https://rawgit.com/ajaxorg/ace-builds/master/src/ace.js"></script>
<script src="https://rawgit.com/ajaxorg/ace-builds/master/src/ext-language_tools.js"></script>
<link   rel="import" href="https://rawgit.com/Download/polymer-cdn/master/lib/polymer/polymer.html">
<link   rel="import" href="https://rawgit.com/LostInBrittany/ace-widget/master/ace-widget.html">
<link   rel="import" href="https://rawgit.com/Download/polymer-cdn/master/lib/paper-elements/paper-elements.html">


<ace-widget id="editor1" initial-focus theme="ace/theme/monokai" mode="ace/mode/json" softtabs="true" wrap="true">
            This is a nice widget... and we are writing a long text here to show the effects of the `wrap` attribute.
</ace-widget>       
<paper-button raised class="custom indigo" onclick="myrun()">Run</paper-button>             
<paper-textarea label="autoresizing textarea input"></paper-textarea>
<script type="text/javascript">
    function myrun() {
        var editor = document.getElementById("editor1").editor
        var myVar = editor.session.getValue();
        myVar= myVar.replace(/\d+/, '');
        myVar = myVar.substring(0,myVar.indexOf('XXXXXXX'));
        alert(myVar);
    }
</script>
&#13;
&#13;
&#13;