1 回答
TA贡献1770条经验 获得超3个赞
你只需要检查是否已经有一个br标签
您可以通过以下方式检查
if (node.nextElementSibling.tagName.toLowerCase() !== "br") {
// add br
}
工作示例
function insertAfter(newNode, referenceNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}
function writeMess(node, mess){
elementEx= document.getElementsByTagName("br").length;
if(elementEx < 5){
newerrmess = document.createTextNode(mess);
node.replaceChild(newerrmess, node.firstChild);
if (node.nextElementSibling.tagName.toLowerCase() !== 'br') {
br = document.createElement("br");
insertAfter(br, node);
}
}
}
function Add(){
try{
debugger;
writeMess(nodoMessErr1, "");
var capsule = parseInt(nodoCapsule.value);
/* Check correct values */
if(!isNaN(capsule)){
totcapsule = capsule;
}
else{
/* Err Mess */
writeMess(nodoMessErr1, "Error Mess 1");
return;
}
}
catch( e ){
alert("Aggiunta " + e);
return;
}
}
var nodoAdd;
var nodoCapsule;
var nodoMessErr1;
var totcapsule;
/* Core function */
function gestoreLoad(){
debugger;
try{
nodoAdd = document.getElementById("aggiunta");
nodoCapsule= document.getElementById("capsule");
nodoMessErr1 = document.getElementById("adderr");
nodoAdd.onclick = Add;
nodoCapsule.value = "";
var TextNodeErr_1= document.createTextNode("");
nodoMessErr1.appendChild(TextNodeErr_1);
}
catch(e){
alert("gestoreLoad " + e);
}
}
window.onload = gestoreLoad;
<body>
<span id="adderr"></span>
<input type="text" id="capsule" />
<input type="button" id="aggiunta" value="Add"/>
</body>
- 1 回答
- 0 关注
- 87 浏览
添加回答
举报