我在javascript文件中有一个span标记,就像那样
<input type="text" id="name" onblur="submitFormEmail()"/>
<span class="error">This is an error</span>
这是它在css中的风格
.form_wrapper span.error{
visibility:hidden;
color:red;
font-size:11px;
font-style:italic;
display:block;
margin:4px 30px;
}
如何在调用函数submitFormEmail()??
时更改跨度的可见性function submitFormEmail(){
}
答案 0 :(得分:0)
只需
document.getElementsByClassName(".error")[0].style.visibility="visible";
答案 1 :(得分:0)
要在您的功能中调用它,您可以执行以下操作:
function submitFormEmail(){
document.querySelector('.error').style.visibility = 'visible';
}
答案 2 :(得分:0)
假设有许多input
元素,那么函数应该找出哪个节点匹配。
function submitFormEmail(obj) {
var nextSpan = obj.nextSibling;
while(nextSpan.nodeType != 1){
nextSpan = nextSpan.nextSibling;
}
nextSpan.style.visibility = 'visible';
}
.error {
visibility: hidden;
color: red;
font-size: 11px;
font-style: italic;
display: block;
margin: 4px 30px;
}
<input type="text" id="name" onblur="submitFormEmail(this)" /> <span class="error">This is an error</span> <br/>
<input type="text" id="name1" onblur="submitFormEmail(this)" /> <span class="error">This is an error</span> <br/>
<input type="text" id="name2" onblur="submitFormEmail(this)" /> <span class="error">This is an error</span>