人們向我推薦我應該將此代碼轉換爲jquery,但是如何以及在哪裏,是否有任何工具或者如何執行此操作以及爲什麼XMLHttpRequest不適合jQuery?如何將普通JavaScript代碼轉換爲jQuery
我有這樣的代碼:
function showVal(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "* Please type in School Name.";
return;
}
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) { // break this into 2 statements so you can handle HTTP errors
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
} else {
document.getElementById("txtHint").innerHTML = "AJAX Error (HTTP "+xmlhttp.status+")";
}
}
}; // functions declared in this way should be followed by a semi colon, since the function declaration is actually a statement.
// encodeURIComponent() does all the escaping work for you - it is roughly analogous to PHP's urlencode()
// xmlhttp.open("GET","ajaxFuncs2.php?q="+encodeURIComponent(str),true);
xmlhttp.open("GET","ajaxFuncs2.php?q="+encodeURIComponent(str),true);
xmlhttp.send();
}
你應該瞭解jQuery的一些文檔開始。從官方網站開始http://docs.jquery.com/How_jQuery_Works – 2012-01-17 17:09:46
如果有效,可能沒有任何理由需要切換。事實上,如果您現在不使用(並加載)jQuery,那麼轉換爲jQuery只會增加頁面的大小。 – 2012-01-17 17:10:06
它不適用於我的其他jQuery代碼:S – Drazek 2012-01-17 17:12:39