【问题标题】:How to highlight/mark any text passages, so that javascript can change it?如何突出显示/标记任何文本段落,以便 javascript 可以更改它?
【发布时间】:2016-03-28 19:59:49
【问题描述】:

更具体一点:用户应该能够突出显示文本,以便 Javascript 可以用文本区域替换这个突出显示的部分。我的问题是,我不知道如何编辑文本的任何部分!

我希望我的示例图有助于理解我的非常具体的问题!

【问题讨论】:

    标签: javascript user-interface highlight


    【解决方案1】:

    我在 JSFiddle 中构建了一个小脚本,链接如下:http://jsfiddle.net/WT5XE/2/

    JS

    var textB = ''; // text before split
    var textA = ''; // text after split
    
    ...
    
    // here starts the magic, split current text and insert a new input box
    var splitted = that.innerHTML.split(text);
    textB = splitted[0];
    textA = splitted[1];
    
    $('#text').empty();
    // Append the text before in a new span
    $('#text').append('<span>'+textB+'</span>');
    // Create the input box and attach key listener (on enter it will terminate and set the new text)
    $('<input value="'+text+'">')
        .keypress(function (e) {
            if (e.which == 13) {
                isInput = false;
                var newText = this.value;
                var text = textB+newText+textA;
                $('#text').empty();
                $('#text').append(text);
                return false;
            }
        })
        .appendTo('#text');
    // Append the text after in a new span
    $('#text').append('<span>'+textA+'</span>');
    

    【讨论】:

      猜你喜欢
      • 2010-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多