msnWebSearch.drawResultBox = function (e) {
var divSearchBox= document.createElement("div");
var divHeading = document.createElement("div");
var divResultsPane = document.createElement("div");
var aCloseLink = document.createElement("a");
后面这些代码经过createElement()办法创立HTML元素。在创立这些元素后,你就可以够入手下手付与它们属性。下面完成终结(封尾)的两个元素分离是aCloseLink和divHeading:
msnWebSearch.drawResultBox = function (e) {
var divSearchBox= document.createElement("div");
var divHeading = document.createElement("div");
var divResultsPane = document.createElement("div");
var aCloseLink = document.createElement("a");
aCloseLink.href = "#";
aCloseLink.className = "ajaxWebSearchCloseLink";
aCloseLink.onclick = this.close;
aCloseLink.appendChild(document.createTextNode("X"));
divHeading.className = "ajaxWebSearchHeading";
divHeading.appendChild(document.createTextNode("MSN Search Results"));
divHeading.appendChild(aCloseLink);
var divLoading = document.createElement("div");
divLoading.appendChild(document.createTextNode("Loading Search Feed"));
divResultsPane.className = "ajaxWebSearchResults";
divResultsPane.appendChild(divLoading);
divSearchBox.className = "ajaxWebSearchBox";
divSearchBox.appendChild(divHeading);
divSearchBox.appendChild(divResultsPane);
document.body.appendChild(divSearchBox);
this.position(e, divSearchBox);
return divSearchBox;
};
经由过程这类体例创立msnWebSearch对象后,必需把divSearchBox前往到它的挪用者以便停止其它操作。你可以已猜出,position()办法担任放置该搜刮框。它承受两个参数:传递到drawResultBox()的事务对象和divSearchBox元素:
msnWebSearch.position = function (e, divSearchBox) {
var x = e.clientX + document.documentElement.scrollLeft;
var y = e.clientY + document.documentElement.scrollTop;
divSearchBox.style.left = x + "px";
divSearchBox.style.top = y + "px";
};
前两行代码失掉右边和顶部地位,用于放置搜刮了局框。履行这个操作请求利用两种信息。起首是鼠标的x和y坐标(这些信息被存储在clientX和clientY属性)。