最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

dom - javascript returning object htmlelement - Stack Overflow

programmeradmin2浏览0评论

I'm working on a calculator application. I created the following function to push the the current function to a div to display itself.

function addtoScreen(vTag){

      var screen = document.getElementById("screen");

      if(currentFunction.length == 1){

        var newCalc = document.createElement("p");    
        newCalc.setAttribute("class", "calc");

        var opInt = "<span>"+vTag+"</span>";

        newCalc.innerHTML = opInt;
        screen.innerHTML = newCalc;

      }else{
        var opInt = "<span>"+vTag+"</span>";
        newCalc = document.getElementById("screen").lastElementChild;
        newCalc.innerHTML = opInt;  

      }
    }

The if portion keeps returning [object HTMLParagraphElement] what am I doing wrong? I have tried to use .value but that just returns a null because the object is a entire element that does not have a value attribute.

Edit: I didn't mention that vTag is just a string being passed through from a function that pushs to a array.

I'm working on a calculator application. I created the following function to push the the current function to a div to display itself.

function addtoScreen(vTag){

      var screen = document.getElementById("screen");

      if(currentFunction.length == 1){

        var newCalc = document.createElement("p");    
        newCalc.setAttribute("class", "calc");

        var opInt = "<span>"+vTag+"</span>";

        newCalc.innerHTML = opInt;
        screen.innerHTML = newCalc;

      }else{
        var opInt = "<span>"+vTag+"</span>";
        newCalc = document.getElementById("screen").lastElementChild;
        newCalc.innerHTML = opInt;  

      }
    }

The if portion keeps returning [object HTMLParagraphElement] what am I doing wrong? I have tried to use .value but that just returns a null because the object is a entire element that does not have a value attribute.

Edit: I didn't mention that vTag is just a string being passed through from a function that pushs to a array.

Share Improve this question edited Nov 11, 2014 at 16:50 Mike F asked Nov 11, 2014 at 16:41 Mike FMike F 3091 gold badge8 silver badges19 bronze badges 4
  • 1 please add your html, optionally as snippet. – ps2goat Commented Nov 11, 2014 at 16:43
  • 1 vTag is a DOM element? You should be able to use vTag.outerHTML to get its content – James Thorpe Commented Nov 11, 2014 at 16:43
  • 1 It looks like you are setting your currentFunction.length = 1 rather than paring the length to 1 currentFunction.length == 1. – zack.lore Commented Nov 11, 2014 at 16:45
  • I've made the adjustment – Mike F Commented Nov 11, 2014 at 16:51
Add a ment  | 

1 Answer 1

Reset to default 3

How about something like this (I mented out your old code):

function addtoScreen(vTag){

  var screen = document.getElementById("screen");

  if(currentFunction.length == 1){

    var newCalc = document.createElement("p");    
    //newCalc.setAttribute("class", "calc");
    newCalc.className = "calc";

    //var opInt = "<span>"+vTag+"</span>";
    var opInt = document.createElement("span");
    opInt.innerHTML = vTag;

    //newCalc.innerHTML = opInt;
    newCalc.appendChild(opInt);

    //screen.innerHTML = newCalc;
    screen.appendChild(newCalc);

  }else{
    //var opInt = "<span>"+vTag+"</span>";
    var opInt = document.createElement("span");
    opInt.innerHTML = vTag;

    newCalc = screen.lastChild;

    //newCalc.innerHTML = opInt;
      if(newCalc)
      {
    newCalc.appendChild(opInt);
      }
      else
      {
       screen.appendChild(opInt);   
      }

  }

}

发布评论

评论列表(0)

  1. 暂无评论