2010-10-26 49 views
0

我想了解这段代码,因为我是一个初学者。大多是这些红色的字体。他们正在获取哪个页面值?关于一段代码

$(function() { 
     $("#title").blur(function() { QuestionSuggestions(); });  
}); 

function QuestionSuggestions() { 
    var s = $("#title").val();    
    if (s.length > 2 && !($("#title").hasClass('edit-field-overlayed'))) { 
     document.title = s + " - Stack Overflow"; 
     $("#question-suggestions").load("/search/titles?like=" + escape(s)); 
    } 
} 
+1

2建议改善,它可以只是'$(“#title”)。blur(QuestionSuggestions);'并且不使用encode()',使用'encodeURIComponent()'。 – 2010-10-26 10:52:13

回答

3
function QuestionSuggestions() { 
     var s = $("#title").val(); // Here we take the value of element with ID "title" 
     // If the length of the title is bigger than 2 or 
     // the element doesn't have 'edit-field-overlayed' class  
     if (s.length > 2 && !($("#title").hasClass('edit-field-overlayed'))) { 
      // we set the title of the document as <title>[our old title] - Stack Overflow</title> 
      document.title = s + " - Stack Overflow"; 

      // Load data from the server and place the returned HTML into the matched element. 
      $("#question-suggestions").load("/search/titles?like=" + escape(s)); 
     } 
    } 

如果id为标题元素有更长的标题比2,让说,“我的冠军”,并没有类“编辑场叠加”我们改变了页面的标题为“我的标题 - 堆栈溢出“并通过查询URL加载元素”#问题 - 建议“中的HTML /文本http://yoursite.tld/search/titles?like=My%20title

1

这看起来像jQuery代码。表达式$("#title")是对jQuery $函数的调用。它使用id="title"查找HTML标签并在其周围包装实用程序对象。 .blur是该实用程序对象的一种方法,该对象提供了在鼠标移出相应元素时要调用的函数。

最好的事情就是陷入像this one这样的jQuery教程中。

1

代码的peice的张贴,冷凝成句子是

“当ID为‘标题’模糊领域中,执行一个Ajax查询传递该字段的内容作为参数”