调用的代码呢,则是相当简单,不需要创建其他的Label或者span标签,脚本将自动生成:
//验证不能为空展示提示信息 function checkResult(checkCondition, showAfterId, showMsg) { var showLabelId = showAfterId + "showMsg"; if (checkCondition) { if (document.getElementById(showLabelId)) { document.getElementById(showLabelId).innerHTML = showMsg; } else { createShowElement(showAfterId, showLabelId, "color:red", showMsg, 'span'); } } else if (!checkCondition) { if (document.getElementById(showLabelId)) document.getElementById(showLabelId).innerHTML = ''; } }
好,最后我们来看这个“createShowElement(currentId, elementId, style, showMsg, tagName)”函数:currentId即当前标签的ID;elementId为创建的标签的ID;style为创建的标签的样式,按照样式的写法即可;showMsg不讲了;tagName为创建的标签名,如label或者span等。
//创建展示提示信息的dom function createShowElement(currentId, elementId, style, showMsg, tagName) { if (!tagName) tagName = 'label'; var currentDom = document.getElementById(currentId); var showMsgDom = document.createElement(tagName); //showMsgDom.setAttribute("style", "color:" + textColor + ";"); if (style) showMsgDom.setAttribute("style", style); showMsgDom.setAttribute("id", elementId); showMsgDom.innerHTML = showMsg; currentDom.parentNode.insertBefore(showMsgDom, currentDom.nextSibling); }