最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

innerhtml - Showing the contents of a SPAN using JavaScript - Stack Overflow

matteradmin5PV0评论

I have this code. I've done this for years now but I'm stumped with the result of this example. The purpose is to make the text box visible and put the contents of the clicked SPAN tag in it.

document.onclick = CaptureClickedElement;

function CaptureClickedElement(e)
{
var EventElement;
if(e==null)
    EventElement = event.srcElement;// IE
else
    EventElement = e.target;// Firefox

if( EventElement.tagName == "SPAN")
    {
    document.getElementById("divTXT").style.display="";
    document.getElementById("txt").value = document.getElementById("Span1").innerHTML;
    alert(document.getElementById("Span1").innerHTML)
    }
}

Strangely though, it DOES show the contents but also shows open/close SPAN tags at the end of it. If I alert ther results, the same thing is shown.

Please find the attached screen shot of it here.

Does anyone have an idea of why this is happening?

Thanks!

Here is the HTML (copied from ments by mplungjan)

<style type="text/css"> 
 #divOuter { 
   width: 100px; 
   height: 70px; 
   border: 1px solid blue; 
   float: left; 
 } 
 </style>

<body> 
  <div> 
    <form name="frm" method="post" action=""> 
      <div id="divTXT" style="display:none"> 
        <input type="text" id="txt" name="txt" value="" size="30" /> 
      </div> 
    </form> 
  </div> 
  <div id="divOuter"> 
    <span id="Span1">hi, this is a test.<span> 
  </div> 
</body>

I have this code. I've done this for years now but I'm stumped with the result of this example. The purpose is to make the text box visible and put the contents of the clicked SPAN tag in it.

document.onclick = CaptureClickedElement;

function CaptureClickedElement(e)
{
var EventElement;
if(e==null)
    EventElement = event.srcElement;// IE
else
    EventElement = e.target;// Firefox

if( EventElement.tagName == "SPAN")
    {
    document.getElementById("divTXT").style.display="";
    document.getElementById("txt").value = document.getElementById("Span1").innerHTML;
    alert(document.getElementById("Span1").innerHTML)
    }
}

Strangely though, it DOES show the contents but also shows open/close SPAN tags at the end of it. If I alert ther results, the same thing is shown.

Please find the attached screen shot of it here.

Does anyone have an idea of why this is happening?

Thanks!

Here is the HTML (copied from ments by mplungjan)

<style type="text/css"> 
 #divOuter { 
   width: 100px; 
   height: 70px; 
   border: 1px solid blue; 
   float: left; 
 } 
 </style>

<body> 
  <div> 
    <form name="frm" method="post" action=""> 
      <div id="divTXT" style="display:none"> 
        <input type="text" id="txt" name="txt" value="" size="30" /> 
      </div> 
    </form> 
  </div> 
  <div id="divOuter"> 
    <span id="Span1">hi, this is a test.<span> 
  </div> 
</body>
Share Improve this question edited May 1, 2011 at 5:57 mplungjan 179k28 gold badges182 silver badges240 bronze badges asked May 1, 2011 at 5:42 itsolsitsols 5,5927 gold badges56 silver badges97 bronze badges 2
  • <style type="text/css"> #divOuter { width: 100px; height: 70px; border: 1px solid blue; float: left; } </style> – itsols Commented May 1, 2011 at 5:51
  • <body> <div> <form name="frm" method="post" action=""> <div id="divTXT" style="display:none"> <input type="text" id="txt" name="txt" value="" size="30" /> </div> </form> </div> <div id="divOuter"> <span id="Span1">hi, this is a test.<span> </div> </body> – itsols Commented May 1, 2011 at 5:52
Add a ment  | 

2 Answers 2

Reset to default 3

Structure problem:

<span id="Span1">hi, this is a test.<span>

Note the absence of a proper close to the span.

Need to use .innerText (for IE) and .text for others.

Post a comment

comment list (0)

  1. No comments so far