0
嘿,我想根據結果更改字體顏色或responseText。例如,如果responseText未找到,我希望字體顏色爲紅色。否則,它會變黑。它目前顯示正確的responseText;我只是想在必要時改變顏色。這是我目前的Ajax:Ajax或JavaScript:根據服務器響應改變樣式
function newXMLHttpRequest()
{
var xmlreq = false;
if (window.XMLHttpRequest)
{
xmlreq = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
try
{
xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e2)
{
alert("Error: Unable to create an XMLHttpRequest.");
}
}
return xmlreq;
}
function getLocation(location)
{
var getLocation= newXMLHttpRequest(); // sending request
getLocation.open("GET", "/PP?PAGE=GETLOCATIONNAME&ROUTINGNUM=" + location, false);
getLocation.send(null); // getting location
document.getElementById("location_div").innerHTML = getLocation.responseText;
}
這個responseText將在HTML表格內:
<tr>
<td class="ajax_msg">
<div id="location_div"></div>
</td>
<td> </td>
</tr>
<tr>
<td>
<div class="column1">
<p class="label_top">
*Routing Number</p>
<p class="field_top"><input type="text" id="location" name="location" size="28" maxlength="9" onblur="getLocation(this.value);" /></p>
任何建議都歡迎。提前致謝!
感謝您的答覆。不知道爲什麼,但它不起作用。如果我註釋掉在檢查這樣的響應: \t \t功能的getLocation(位置) \t \t { \t \t變種的getLocation = newXMLHttpRequest(); getLocation.open(「GET」,「/ PP?PAGE = GETLOCATIONNAME&ROUTINGNUM =」+ location,false); \t \t getLocation.send(null); \t \t \t \t \t var dv = document.getElementById(「location_div」); \t \t \t \t \t //如果(getLocation.responseText == '未找到') \t \t // { \t \t \t dv.style.color = 「紅」; \t \t //} \t \t dv.innerHTML = getLocation.responseText; \t \t} 那麼字體是紅色的。到現在爲止還挺好。 – Spockrates 2010-08-09 19:46:50
但是如果我不評論它是這樣的: \t \t功能的getLocation(位置) \t \t { \t \t VAR的getLocation = newXMLHttpRequest(); getLocation.open(「GET」,「/ PP?PAGE = GETLOCATIONNAME&ROUTINGNUM =」+ location,false); \t \t getLocation.send(null); \t \t \t \t \t var dv = document.getElementById(「location_div」); \t \t \t \t \t如果(getLocation.responseText == '未找到') \t \t { \t \t \t dv.style.color = 「紅」; \t \t} \t \t dv.innerHTML = getLocation.responseText; \t \t} 然後字體保持黑色。任何想法,爲什麼它不適用風格? – Spockrates 2010-08-09 19:48:21
請參閱我的更新。 – Sarfraz 2010-08-09 19:52:43