Jquery regex replace()只运行两次.然后不# 39;t
jquery regex replace() only runs twice... then doesn't
这个问题让我感到羞愧和沮丧。我写的应该是一个非常简单的脚本,删除"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);
}
}
相关文章:
- Meteor Router数据函数被调用两次
- 从MySQL数据库中获取输入数据需要两次页面刷新
- Module.start()已激发两次
- Jquery点击事件必须点击两次
- Append元素在运行两次函数后不显示
- 防止双击执行两次jQuery post请求
- 我必须点击两次才能激活任何按钮操作(离子/角度)
- 单击jQuery会激发两次
- 如何避免在树上走两次
- button.单击两次删除附加操作后不工作
- Javascript onclick需要点击两次,然后会创建越来越多的服务器请求
- JavaScript onclick在IE 9中工作两次,然后停止
- Chrome扩展程序 - 页面更新两次,然后在YouTube上删除
- 加载时可以单击按钮#1,然后需要单击按钮#2两次.按钮#1也需要点击两次
- 地图,rails 4.2,javascript,鼠标悬停,只工作一次(或两次).然后在重新加载之后
- 在同一元素上单击两次,然后在另一个元素上单击两次,使第一个元素进入“单击”状态
- Javascript幻灯片循环两次很好,然后出现错误
- Jquery regex replace()只运行两次.然后不# 39;t
- 如果切换img src然后再返回,图像将被加载两次
- 为什么我的React组件渲染调用两次,一次没有数据,然后有数据,但太晚的例外