jQuery/javascript:webkit clearTimeout问题
在我上一个问题的后面,我对webkit浏览器Safari和Chrome中的以下代码有一些问题:-jQuery/javascript:webkit clearTimeout问题,javascript,jquery,google-chrome,safari,webkit,Javascript,Jquery,Google Chrome,Safari,Webkit,在我上一个问题的后面,我对webkit浏览器Safari和Chrome中的以下代码有一些问题:- // Textarea focus out event. var renderHandler; $("textarea").live('focusout', function (e) { var currentNote = this; renderHandler = setTimeout( function(){ renderNote(currentNote); }, 100); }
// Textarea focus out event.
var renderHandler;
$("textarea").live('focusout', function (e) {
var currentNote = this;
renderHandler = setTimeout( function(){ renderNote(currentNote); }, 100);
});
// handle exceptions
$('.textEdit').live('focusin', function (e) {
clearTimeout(renderHandler);
});
function renderNote( note ){
var itemContent = $(note).val();
itemContent = htmlStrip(itemContent);
itemContent = itemContent.replace(/\n/g, "<br/>"); // New lines
//itemContent = itemContent.replace(/\s/g, " "); // Spaces
// Formatting replacements
itemContent = itemContent
.replace(/\[b\]/gi, "<b>")
.replace(/\[\/b\]/gi, "</b>")
.replace(/\[i\]/gi, "<i>")
.replace(/\[\/i\]/gi, "</i>")
.replace(/\[s\]/gi, "<s>")
.replace(/\[\/s\]/gi, "</s>");
$(note).replaceWith("<p class='notes'>"+ itemContent +"</p>");
}
//text区域焦点输出事件。
var renderHandler;
$(“textarea”).live('focusout',函数(e){
var currentNote=此;
renderHandler=setTimeout(函数(){renderNote(currentNote);},100);
});
//处理异常
$('.textEdit').live('focusin',函数(e){
clearTimeout(renderHandler);
});
函数renderNote(注){
var itemContent=$(注意).val();
itemContent=htmlStrip(itemContent);
itemContent=itemContent.replace(//\n/g,“
”);//新行
//itemContent=itemContent.replace(//\s/g,“”;//空格
//格式替换
itemContent=itemContent
.替换(/\[b\]/gi,“”)
.替换(/\[\/b\]/gi,“”)
.替换(/\[i\]/gi,”)
.替换(/\[\/i\]/gi,“”)
.替换(/\[s\]/gi,“”)
.替换(/\[\/s\]/gi,”);
$(注)。替换为(“”+itemContent+”
”;
}
在firefox中,renderHandler上的最新clearTimeout阻止调用函数“renderNote”,该函数允许我处理focusout事件的异常。然而,在webkit浏览器中,renderNote被调用
我试过返回、返回错误、预防默认、停止播放、中断,但没有快乐。以前有人见过这个吗
这里有一个链接:
如果双击正文,然后单击便笺的正文,您可以看到它正在运行。注意,
$.live()
已弃用<代码>$.on()或$.delegate()
应改为使用。由于某种原因,这两种方法的正确使用一直回避着我,所以我不能提出如何做到这一点,但是你应该考虑避免<代码> $LIVER()>代码>,因为它最终会被删除,这是一个性能问题。
据我所知,这句话:
$('.textEdit').live('focusin', function (e) {
从不在铬合金中运行。没关系,因为看起来
$('.textEdit').live('click', function (e) {
应该很好用
我将修改您的方法,并在中使用块变量,而不是取消超时。例如:
var renderHandler,
blockRender = false;
$("textarea").live('focusout', function (e) {
var currentNote = this;
renderHandler = setTimeout(function(){
renderNote(currentNote);
}, 100);
});
$('.textEdit').live('click', function (e) {
blockRender = true;
});
然后在renderNote()
中:
函数renderNote(注释){
var itemContent=$(注意).val();
如果(块渲染){
blockRender=false;
返回false;
}
itemContent=htmlStrip(itemContent);
itemContent=itemContent.replace(/\n/g,“
”);
itemContent=itemContent
.替换(/\[b\]/gi,“”)
.替换(/\[\/b\]/gi,“”)
.替换(/\[i\]/gi,”)
.替换(/\[\/i\]/gi,“”)
.替换(/\[s\]/gi,“”)
.替换(/\[\/s\]/gi,”);
$(注)。替换为(“”+itemContent+”
”;
}
Working fiddle demo:实际上,我看到上面的代码是
functionality.js
的一部分。Try:相同的不同点mate他们都展示了一个工作示例:)是的,但第一个示例无法由感兴趣的SOer编辑,因为它包含在.ready()
块中。如果有人单击其中一种文本样式(删除线、粗体、斜体),您尝试的是阻止渲染的内容吗?“正确使用这两个选项中的任何一个都会让我感到困惑”…我想我一定是唯一的一个。当用作“实时”替代品时,它似乎永远不会起作用,例如,$('body')。on('event','selector',func)
。仍然没有弄清楚我是不是很愚蠢,还是有什么不对的地方。这可能会有所帮助:@kryponidave,实际上这个答案给出的例子确实很有用。jquery.com上的文档很糟糕,甚至没有一个替换“live”的基本示例“。谢谢你的链接。@kryptoniedve-我有一头狂野的头发,决定重写整件事(JS-wise)。我现在注意到的唯一一件事是,它不能处理直接从注释编辑到标题编辑的问题(这两个条目我都改为双击)。让我知道您的想法:是的,我看到了太多的Jared,说实话,您的代码看起来比我的代码好得多,更干净整洁,不依赖于折旧事件;)我会看一看,看看我能学到什么!非常感谢!
function renderNote( note ){
var itemContent = $(note).val();
if (blockRender) {
blockRender = false;
return false;
}
itemContent = htmlStrip(itemContent);
itemContent = itemContent.replace(/\n/g, "<br/>");
itemContent = itemContent
.replace(/\[b\]/gi, "<b>")
.replace(/\[\/b\]/gi, "</b>")
.replace(/\[i\]/gi, "<i>")
.replace(/\[\/i\]/gi, "</i>")
.replace(/\[s\]/gi, "<s>")
.replace(/\[\/s\]/gi, "</s>");
$(note).replaceWith("<p class='notes'>"+ itemContent +"</p>");
}