我试图让代码运行但没有任何反应,因为我不确定我是否必须调用该函数或它是否自动运行。
当使用 window.onload 方法时,我会给函数一个名称 < init > 并且它会运行。用 jquery 调用它我不确定我是否应该有一个函数名?
请问我应该如何让代码运行。
// JavaScript Document
$(document).ready(function () {
var xhr = false;
var xPos, yPos;
function () {
var allLinks = document.getElementsByTagName("a");
for (var i = 0; i < allLinks.length; i++) {
allLinks[i].onmouseover = showPreview;
}
} //end function
function showPreview(evt) {
if (evt) {
var url = evt.target;
} else {
evt = window.event;
var url = evt.srcElement;
}
xPos = evt.clientX;
yPos = evt.clientY;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else {
if (window.ActiveXObject) {
try {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (xhr) {
xhr.onreadystatechange = showContents;
xhr.open("GET", url, true);
xhr.send(null);
} else {
alert("Sorry, but I couldn't create an XMLHttpRequest");
}
return false;
}
function showContents() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var outMsg = xhr.responseText;
} else {
var outMsg = "There was a problem with the request " + xhr.status;
}
var prevWin = document.getElementById('previewWin');
prevWin.innerHTML = outMsg;
prevWin.style.top = parseInt(yPos) + 2 + "px";
prevWin.style.left = parseInt(xPos) + 2 + "px";
prevWin.style.visibility = "visible";
preview.onmouseout = function () {
document.getElementById('preview').style.visibility = "hidden";
}
}
}
});