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

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

programmeradmin2浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论