构造文件

hnsongbiao 2008-11-23 09:42:44


var $GLOBAL_VAR={
"_text":"",
"_arr":null,
"_count":0,
"_num":0,
"_select":null,
"_out_select":null,
"_flag":true
};
var $callback={
"hidden":function(){
if($GLOBAL_VAR._flag)
$dt._div.style.display = "none";
},
"input_event":function(event){
var _e = event || window.event;
if ($GLOBAL_VAR._text == $dt._tbox.value || _e.keyCode == "40" || _e.keyCode == "38") {
$o.move(_e);
} else {
$GLOBAL_VAR._count=0;
$GLOBAL_VAR._text = $dt._tbox.value;
if(_e.keyCode=="13"){
$callback.hidden();
}else{
$util.createElement($o.updatelist);
}
}
}
};
var Operator=function(){
this.updatelist=function(){
if(text.length<1){
$dt._div.style.display = "none";
return;
}
$GLOBAL_VAR._arr = text.split("|");
var _t;
var _html = "";
for (var _i = 0; _i < $GLOBAL_VAR._arr.length-1; _i++) {
_t=$GLOBAL_VAR._arr[_i].split(",");
_html+=$pro._htmls[0]+_t[0]
+$pro._htmls[1]+(_i+1)
+$pro._htmls[2]+_t[1]
+$pro._htmls[3]+_t[0]
+$pro._htmls[4];
}
_html+=$pro._htmls[5];

$dt._div.innerHTML = _html;

$dt._div.style.display = "block";
$dt._div.onmouseover=function(){
$GLOBAL_VAR._flag=false;
};
$dt._div.onmouseout=function(){
$GLOBAL_VAR._flag=true;
};
$GLOBAL_VAR._num = $GLOBAL_VAR._arr.length - 1;
};
this.xg=function(){
$GLOBAL_VAR._arr = text.split("|");
var _t;
var _html = "";
for (var _i = 0; _i < $GLOBAL_VAR._arr.length-1; _i++) {
if(_i==10)
continue;
_t=$GLOBAL_VAR._arr[_i].split(",");
if(_t[0].length>10 || _t[0]==$dt._tbox.value)
continue;
_html+=$pro._xghtmls[0]+_t[0]
+$pro._xghtmls[1]+_t[0]
+$pro._xghtmls[2];
}
if(_html.length<1)
$util.get("CorrSearch").style.display="none";
return _html;
};
this.move=function(_e){
if (_e.keyCode == "40") {
$GLOBAL_VAR._count++;
if ($GLOBAL_VAR._count > $GLOBAL_VAR._num) {
$GLOBAL_VAR._count = 1;
}
this.moveDown();
}
if (_e.keyCode == "38") {
$GLOBAL_VAR._count--;
if ($GLOBAL_VAR._count < 1) {
$GLOBAL_VAR._count = $GLOBAL_VAR._num;
}
this.moveUp();
}
};
this.moveUp=function(){
$GLOBAL_VAR._select = $util.get("d_"+$GLOBAL_VAR._count);

if($GLOBAL_VAR._count==$GLOBAL_VAR._num)
$GLOBAL_VAR._out_select = $util.get("d_1");
else
$GLOBAL_VAR._out_select = $util.get("d_"+($GLOBAL_VAR._count+1));
$GLOBAL_VAR._out_select.style.backgroundColor="";
$GLOBAL_VAR._select.style.backgroundColor = "#efefef";
$dt._tbox.value = $GLOBAL_VAR._arr[$GLOBAL_VAR._count-1].split(",")[0];
};
this.moveDown=function(){
$GLOBAL_VAR._select = $util.get("d_"+$GLOBAL_VAR._count);

if($GLOBAL_VAR._count==1)
$GLOBAL_VAR._out_select = $util.get("d_"+$GLOBAL_VAR._num);
else
$GLOBAL_VAR._out_select = $util.get("d_"+($GLOBAL_VAR._count-1));
$GLOBAL_VAR._out_select.style.backgroundColor="";
$GLOBAL_VAR._select.style.backgroundColor = "#efefef";
$dt._tbox.value = $GLOBAL_VAR._arr[$GLOBAL_VAR._count-1].split(",")[0];
};
this.selectText=function(_d){
$GLOBAL_VAR._flag=true;
$dt._tbox.value=_d.title;
$callback.hidden();
};
this.over=function(_d){
_d.style.backgroundColor = "#efefef";
};
this.out=function(_d){
_d.style.backgroundColor = "";
};

};
var MyUtil=function(){
this.get=function(_id){
return document.getElementById(_id);
};

this.createElement=function(_callback){
if($dt._tbox.value.length<1){
$dt._div.style.display = "none";
return;
}
var flag=function(){if(_callback==null) return;};
var _element=null;
var _e=this.get($pro._script_id);
if(_e)
_e.parentNode.removeChild(_e);

_element=document.createElement("script");
_element.id=$pro._script_id;
_element.src = $pro._action+($dt._tbox.value);
_element.type = "text/javascript";
document.getElementsByTagName("head")[0].appendChild(_element);

if(document.all){
_element.onreadystatechange = function(){//IE用
var state = _element.readyState;
if (state == "loaded" || state == "interactive" || state == "complete") {
if(_callback==null){
return;
}
_callback();
}
};
} else {
flag();
_element.onload = _callback;//FF用
}
};
};
var Properties={
"_action" : "http://search.mysite.com/ks?keyword=",
"_input" : "keyword",
"_div" : "Se_tip",
"_script_id" : "_rs",
"_htmls": new Array(
"<div onmouseover=\"$o.over(this)\" onclick=\"$o.selectText(this)\" title=\"",
"\" onmouseout=\"$o.out(this)\" id=\"d_",
"\" class=\"Se_tip_item\"><em>",
"条</em><span>",
"</span></div>",
"<div class=\"Se_tip_close\"><a href=\"javascript:void(0)\" onclick=\"javascript:this.parentNode.parentNode.style.display='none';\">关闭</a></div>"
),
"_xghtmls": new Array(
"<div><a href=\"Search?keyword=",
"\">",
"</a></div>"
)

};

var Dt=function (t,d){
this._div=d;
this._tbox=t;
};
var $util=new MyUtil();
var $pro=Properties;
var $o=new Operator();


var $dt=new Dt($util.get($pro._input),$util.get($pro._div));
$dt._tbox.onkeyup=$callback.input_event;
$dt._tbox.onblur=$callback.hidden;
$util.createElement(null);




昨日面试遇到一个构造问题,请高手帮忙,解决这个问题就可获得一份工作。

上面是一个搜索的JS文件,页面有一个搜索框,当输入关键词时,自动显示匹配的词语。

请构造http://search.mysite.com/ks?keyword= 的KS文件

传值的结果形式
var text='诺亚舟,24|亚都加,41|亚都YJ/D23W,1122|诺亚州,65|诺亚洲,24|亚都E350,775|诺基亚,219|诺基亚N78,518|诺基亚5310,223|诺基亚8800,231|诺基亚6122,222|';


...全文
168 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
tian_fang 2008-11-25
  • 打赏
  • 举报
回复
学习
zcl26 2008-11-25
  • 打赏
  • 举报
回复
楼主也很强啊,呵呵,好长的js代码,我用js比较少,只是asp.net解决不了的才用js的
曾默 2008-11-25
  • 打赏
  • 举报
回复
不错的东西 帮顶顶
mengxj85 2008-11-23
  • 打赏
  • 举报
回复
顶一下,JS菜鸟阶段
gongsun 2008-11-23
  • 打赏
  • 举报
回复
up一下。
gengwanshanreally 2008-11-23
  • 打赏
  • 举报
回复
up!learn!

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧