English 中文(简体)
关于 替换 的 替换 Text 函数
原标题:about a replaceText function

在下面的 jQuery 替换Text 函数中, 请给我解释下行 remove. long & amp;& $( remove). remove (); does? 我不懂 & amp; & amp; 的用法 。

$.fn.replaceText = function( search, replace, text_only ) {  
  return this.each(function(){  
    var node = this.firstChild,  
      val,  
      new_val,  
      remove = [];  
    if ( node ) {  
      do {  
        if ( node.nodeType === 3 ) {  
          val = node.nodeValue;  
          new_val = val.replace( search, replace );  
          if ( new_val !== val ) {  
            if ( !text_only && /</.test( new_val ) ) {  
              $(node).before( new_val );  
              remove.push( node );  
            } else {  
              node.nodeValue = new_val;  
            }  
          }  
        }  
      } while ( node = node.nextSibling );  
    }  
    remove.length && $(remove).remove();  
  });  
};  
最佳回答

由于短路条件, $(remove).remove (); 只有当 remove. true-ish 时,才会对 进行评估。 的长度 是 true-ish

由于remove 被宣布为数组,因此基本上与下列数组相同:

if(remove.length != 0)
   $(remove).remove();
问题回答

这里的意思是, $( remove).remove () 只有在 remove. later 不是0的情况下才会执行。

这是因为 是短路操作员, 所以它不会评估其后发生的事情, 如果它之前评价的东西是假的的话 。

虽然你得到了一些正确的答案 有关它做什么:

if(remove.length) // length is bigger than 0 (in this context)
   $(remove).remove();

I must say that code is stupid, jQuery knows how to handle empty sets.
So when $(remove).length == 0 the code $(remove).remove(); just won t do anything.

您只需使用它, 无需 if :

$(remove).remove();

上面写道:

如果删除. 长度返回 0 (相当于假) 以外的东西, 则删除元素

它正在确保“ 移动” 对象被设置

if(remove.length != 0)

以便它不试图删除不存在的东西。 在 jQuery 中, 如果对象$( remove) 不存在, 则. 长度函数返回 0, 与 Javascript 中的 False 相吻合 。

如果它确实存在( 删除. long & gt; 0), 则使用.remove () 函数将其删除。 命名肯定模糊不清 。

它基本上是一行条件说明,通过调用.remove()来阻止不存在的物体的例外。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签