Jquery regex replace()只运行两次.然后不# 39;t

jquery regex replace() only runs twice... then doesn't

本文关键字:然后 两次 replace regex 运行 Jquery      更新时间:2023-09-26

这个问题让我感到羞愧和沮丧。我写的应该是一个非常简单的脚本,删除"y",用"I"代替它们。当发生这种情况时,我将带有类的a添加到新的"I"中,以便我可以撤消它。出于某种原因,第一次更换工作…开关恢复工作了……它又好用了……然后停止。我不知道还能做什么,只能把整个丑陋的脚本贴出来。

不要担心粗糙的正则表达式(它只针对某些"y",有时用于写莫霍克)。

编辑:这是jfiddle上的实时版本:http://jsfiddle.net/vkVBk/

$(document).ready(
function() {
    function swap_out_y(x) {
        //alert(x);
        x = x.replace(/y/, "i");
        x = x.replace(/Y/, "I");
        alert('swap_out_y just ran. x = '+x);
        return x;   
    }
    function swap_out_i(x) {
        //alert(x);
        x = x.replace(/i/, "y");
        x = x.replace(/I/, "Y");
        alert('swap_out_i just ran. x = '+x);
        return x;   
    }
    $('body').delegate('.y_to_i', 'click', y_to_i);
    //$('body').delegate('.i_to_y', 'click', i_to_y);
    function y_to_i() {
        $("span.mohawk_word").each(
            function() {
                $(this).html(
                    $(this).text().replace(/(y[aevu])|(y[oe]n)/ig, function(s) {
                        return '<span class="consonant">'+swap_out_y(s)+'</span>'; 
                        })
                );
            }
        );

        $('.y_to_i').undelegate('click');       
        $('.y_to_i').addClass('i_to_y');
        $('.y_to_i').removeClass('y_to_i');
        $('body').delegate('.i_to_y', 'click', i_to_y);
        //alert('A');
    } //end y_to_i

    function i_to_y(){
        $("span.consonant").each(
            function() {
                $(this).html($(this).text().replace(/i/ig, 
                    function(s) {
                        return swap_out_i(s);
                    }
                )//replace...
                );//html
                //$(this).removeClass('replacement_i');
            }//function(){
        );//each(   
        $('.i_to_y').undelegate('click');
        $('.i_to_y').addClass('y_to_i');
        $('.i_to_y').removeClass('i_to_y');
        $('body').delegate('.y_to_i', 'click', y_to_i);
    }
} //function
); //document ready

你在'body'上调用delegate然后在一些类上调用unddelegate而不是body。所以你最终会得到多个委托。

工作小提琴。http://jsfiddle.net/5HdSC/1/

您在i_to_y函数中忘记了您的span,因此它没有span $("span.mohawk_word")来迭代,在您将其删除并用辅音替换两次之后,它就消失了。

编辑实际上你的html中已经有span了,所以你不需要在return中添加额外的span。只要让另一个函数也使用mohawk_word span即可将其替换为mohawk_word可以修复此问题。

        function y_to_i() {
        $("span.mohawk_word").each(
            function() {
                $(this).html(
                    $(this).text().replace(/(y[aevu])|(y[oe]n)/ig, function(s) {
                        return swap_out_y(s); 
                        })
                );
            }
        );

        $('.y_to_i').undelegate('click');        
        $('.y_to_i').addClass('i_to_y');
        $('.y_to_i').removeClass('y_to_i');
        $('body').delegate('.i_to_y', 'click', i_to_y);
        //alert('A');
    } //end y_to_i

    function i_to_y(){
        $("span.mohawk_word").each(
            function() {
                $(this).html(
                    $(this).text().replace(/i/ig, 
                    function(s) {
                        return swap_out_i(s); 
                    }
                ) );                  
            }
        );
        $('.i_to_y').undelegate('click');
        $('.i_to_y').addClass('y_to_i');
        $('.i_to_y').removeClass('i_to_y');
        $('body').delegate('.y_to_i', 'click', y_to_i);
    }
}