javascript - 更改函数的加载顺序会意外破坏代码

标签 javascript jquery

我有一个如下所示的脚本:

function autoCorrect(searchString, replaceString) {
    $("body").on("keyup", "textarea", function() {
        // finds current cursor position
        var pos = $(this).prop("selectionStart");
        // this turns the textarea in a string
        var text = $(this).val();
        //only search for strings just typed
        var stringToSearch = text.substring(pos - searchString.length, pos);
        if (searchString == stringToSearch) {
            //if there is a match put the replaceString in the right place
            var newText = text.substring(0, pos - searchString.length) + replaceString + text.substring(pos);
            $(this).val(newText);
            //adjust the cursor position to the new text
            var newpos = pos - searchString.length + replaceString.length;
            this.setSelectionRange(newpos, newpos);
        }
    });
}
autoCorrect("=>", '⇒');
autoCorrect("->", "→");
autoCorrect("+-", "±");
autoCorrect("<=", "≤");
autoCorrect(">=", "≥");

现在,我想稍微改变一下,我想改变函数运行的顺序。像这样:

$("body").on("keyup", "textarea", function() {
    function autoCorrect(searchString, replaceString) {
        // finds current cursor position
        var pos = $(this).prop("selectionStart");
        // this turns the textarea in a string
        var text = $(this).val();
        //only search for strings just typed
        var stringToSearch = text.substring(pos - searchString.length, pos);
        if (searchString == stringToSearch) {
            //if there is a match put the replaceString in the right place
            var newText = text.substring(0, pos - searchString.length) + replaceString + text.substring(pos);
            $(this).val(newText);
            //adjust the cursor position to the new text
            var newpos = pos - searchString.length + replaceString.length;
            this.setSelectionRange(newpos, newpos);
        }
    }
    autoCorrect("=>", '⇒');
    autoCorrect("->", "→");
    autoCorrect("+-", "±");
    autoCorrect("<=", "≤");
    autoCorrect(">=", "≥");
});

但是这个脚本不再像这样工作了。我只是不明白为什么这会破坏我的代码。

这是我的jsfiddle:http://jsfiddle.net/4SWy6/4/

最佳答案

新函数创建新范围:

$("body").on("keyup", "textarea", function () {
    autoCorrect("=>", '⇒', this);
    autoCorrect("->", "→", this);
    autoCorrect("+-", "±", this);
    autoCorrect("<=", "≤", this);
    autoCorrect(">=", "≥", this);
});

function autoCorrect(searchString, replaceString, elem) {
    var acList = {
        "=>": '⇒',
        "->": "→",
        "+-": "±",
        "<=": "≤",
        ">=": "≥"
    },
        pos = elem.selectionStart,
        text = elem.value,
        stringToSearch = text.substring(pos - searchString.length, pos);

    if (searchString == stringToSearch) {
        var newpos = pos - searchString.length + replaceString.length;
        elem.value = text.substring(0, pos - searchString.length) + replaceString + text.substring(pos);
        this.setSelectionRange(newpos, newpos);
    }
}

FIDDLE

编辑:

基于评论询问您是否可以传递一个键/值对的对象来与元素一起替换,我编写了这个,它应该适用于您传递它的任何对象:

$("body").on("keyup", "textarea", function () {
    var acList = {
        "=>": '⇒',
        "->": "→",
        "+-": "±",
        "<=": "≤",
        ">=": "≥"
    };
    autoCorrect(acList, this);
});

function autoCorrect(acList, elem) {
    var regstr = Object.keys(acList).join("|").replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$]/g, "\\$&"),
        reg    = new RegExp('('+ regstr +')', "gi"),
        pos    = elem.selectionStart,
        diff   = 0;

    elem.value = elem.value.replace(reg, function(x) {
        diff += x.length - acList[x].length;
        return acList[x];
    });

    elem.setSelectionRange(pos-diff, pos-diff);
}

FIDDLE

顺便说一句,这并不是真正的跨浏览器,因为 selectionStartsetSelectionRange 并不在所有浏览器中工作,Object.keys 也不起作用,因此根据您需要支持的浏览器,您可能必须填充这些方法。

关于javascript - 更改函数的加载顺序会意外破坏代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20293119/

相关文章:

javascript - 运行多核机器的 Express JS 应用程序中的全局对象最佳实践

javascript - 如何获取由 .change() 事件触发的多项选择的单击选项值

jquery - 选择2 : default selection in multiple select with AJAX

javascript - 使用Jquery显示link_to,返回JSON数据

jquery - 为什么 HTML 标记中的空格会导致 CSS 不同?

javascript - 如何过滤列表中的项目而不破坏其他过滤器?

javascript - 如何在激活另一个元素时关闭一个元素?

javascript - 获取由 PHP 生成的兄弟 div id

javascript - Angular JS 双重请求

javascript - Jquery- noConflict() 不适合歌剧?