Ajax 或 JavaScript:根据服务器响应更改样式
嘿,我想根据结果更改字体颜色或响应文本。例如,如果未找到响应文本,我希望将字体颜色设置为红色。不然就黑了。它当前显示正确的responseText;我只是想在必要时改变颜色。这是我当前的 Ajax:
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>
欢迎任何建议。提前致谢!
Hey, I'd like to change the font color or the responseText based on the result. For example, if the responseText is NOT FOUND, I'd like to font color to be red. Otherwise, it will be black. It's currently displaying the correct responseText; I just want to change the color when necessary. Here is my current Ajax:
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;
}
The responseText will be within a table in the 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>
Any suggestions are welcome. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样修改您的代码:
您基本上检查条件中的响应文本:
并使用
style
更改其颜色,如下所示:请注意,
dv
变量代表您将所在的 div显示之前使用此行设置的响应文本:更新:
尝试使用其他条件,因为默认情况下您可能具有红色:
Modify your code like this:
You basically check the response text in condition with:
And change its color using
style
like this:Note that
dv
variable represents the div where you will be showing the respones text which was set earlier with this line:Update:
Try with else condition because possibly you have red color by default: