简体   繁体   English

CSS不适用于IE-边框样式更改

[英]CSS not working for IE - border style change

I have a validation code where I want to change the border color to RED of my input text if the input text is blank or onblur. 我有一个验证码,如果输入文本为空白或onblur,我想将输入文本的边框颜色更改为RED。

In FF and Chrome, it works fine. 在FF和Chrome中,效果很好。 In IE10, it doesn't work. 在IE10中,它不起作用。

Here's my script 这是我的剧本

function onblur_style() {
var x = document.forms["myform"]["MySearchKeywordInput"].value;
    if (x != "" || x != null) {
        document.getElementById("MySearchBox").style="border: 2px solid #5A5A5A;color: red";
    } 
}

function validateForm() {
var y = document.forms["myform"]["MySearchKeywordInput"].value;
    if (y==null || y=="") {        
        alert("At least one search query is required.");
        document.getElementById("MySearchBox").style="border: 2px solid #FF0000;color: red";
        return false;
    } else {
        document.forms["myform"].submit();
    }
}

Here's my html 这是我的HTML

<form id="myform" method="get" action="http://submitpage.page" onsubmit="return validateForm()">
<table>
<tr>
<td>
<input type="text" id="MySearchBox" name="MySearchKeywordInput" maxlength="60" size="180" value="Search for solutions" onfocus="if (this.value==this.defaultValue)this.value='';" onblur="onblur_style()">
</td>
<td>
<input type="image" src="http://image/magnifying_glass.gif">
</td>
</tr>
</table>

Any idea? 任何想法?

As mentioned in my comment, you seem to be confusing the style property with the style attribute . 如我的评论所述,您似乎将style 属性style 属性混淆了。

If you want to use the property, try something like this... 如果您想使用该属性,请尝试如下操作...

var searchBoxStyle = document.getElementById("MySearchBox").style;
searchBoxStyle.border = '2px solid #5A5A5A';
searchBoxStyle.color = 'red';

The style property of DOM elements is an object that holds values for different style properties. DOM元素的style属性是一个对象,其中包含不同样式属性的值。 Thus if you want to change the border and color of an element through the style property then you need to do this: 因此,如果要通过style属性更改元素的边框和颜色,则需要执行以下操作:

element.style.border = "2px solid #5A5A5A";
element.style.color = "#FF3030";

If you want to change the style attribute of the element like in your example you need to do this: 如果要像示例中一样更改元素的样式属性,则需要执行以下操作:

element.setAttribute("style", "border: 2px solid #5A5A5A; color: red");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM