Javascript ReplaceChild 不显示消息

我有这个代码


 <!DOCTYPE html>

        <html>

        <head>

        <script>

        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);

                br = document.createElement("br");

                insertAfter(br, node);

            }

        }

        

        

        function Add(){

            try{

        

                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(){

                   }

            catch(e){

                alert("gestoreLoad " + e);

            }

        }


函数 writeMess 充当消息(文本节点)的“编写器”,将其附加为 span 的子级,并在 span 后放置一个“< br >”

看看“Add”函数,我的程序的工作原理就像当用户将 NaN 值放入“nodoCapsule.value”中时显示一条错误消息(默认“”)来提醒他。

我尝试按下按钮(id =“Aggiunta”)并在其中写入 NaN 值,但没有显示任何消息(仅创建了“ < br > ”,但前面没有文本)

不知道为什么,有什么解决办法吗?

编辑 - 这取决于浏览器,其他浏览器向我显示错误消息,但继续创建“<br>”。当显示错误消息时,我只想显示一个“< br >”。没有消息,也没有 br 没有显示消息。


holdtom
浏览 88回答 1
1回答

德玛西亚99

你只需要检查是否已经有一个br标签您可以通过以下方式检查if (node.nextElementSibling.tagName.toLowerCase() !== "br") {&nbsp; &nbsp; &nbsp;// add br}工作示例function insertAfter(newNode, referenceNode) {&nbsp; &nbsp; referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);}function writeMess(node, mess){&nbsp; &nbsp; elementEx= document.getElementsByTagName("br").length;&nbsp; &nbsp; if(elementEx < 5){&nbsp; &nbsp; &nbsp; &nbsp; newerrmess = document.createTextNode(mess);&nbsp; &nbsp; &nbsp; &nbsp; node.replaceChild(newerrmess, node.firstChild);&nbsp; &nbsp; &nbsp; &nbsp; if (node.nextElementSibling.tagName.toLowerCase() !== 'br') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; br = document.createElement("br");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; insertAfter(br, node);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}function Add(){&nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; debugger;&nbsp; &nbsp; &nbsp; &nbsp; writeMess(nodoMessErr1, "");&nbsp; &nbsp; &nbsp; &nbsp; var capsule = parseInt(nodoCapsule.value);&nbsp; &nbsp; &nbsp; &nbsp; /* Check correct values */&nbsp; &nbsp; &nbsp; &nbsp; if(!isNaN(capsule)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; totcapsule = capsule;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; /* Err Mess */&nbsp; &nbsp; &nbsp; &nbsp; writeMess(nodoMessErr1, "Error Mess 1");&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; catch( e ){&nbsp; &nbsp; &nbsp; &nbsp; alert("Aggiunta " + e);&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }}&nbsp; &nbsp;var nodoAdd;var nodoCapsule;var nodoMessErr1;var totcapsule;/* Core function */function gestoreLoad(){debugger;try{&nbsp; &nbsp; nodoAdd = document.getElementById("aggiunta");&nbsp; &nbsp; nodoCapsule= document.getElementById("capsule");&nbsp; &nbsp; nodoMessErr1 = document.getElementById("adderr");&nbsp; &nbsp; nodoAdd.onclick = Add;&nbsp; &nbsp; nodoCapsule.value = "";&nbsp; &nbsp; var TextNodeErr_1= document.createTextNode("");&nbsp; &nbsp; nodoMessErr1.appendChild(TextNodeErr_1);&nbsp; &nbsp; }&nbsp; &nbsp; catch(e){&nbsp; &nbsp; &nbsp; &nbsp; alert("gestoreLoad " + e);&nbsp; &nbsp; }}window.onload = gestoreLoad;<body>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span id="adderr"></span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" id="capsule" />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="button" id="aggiunta" value="Add"/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; </body>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5