简体   繁体   English

无法在Javascript函数中访问span标记的innerHTML

[英]Unable to access span tag's innerHTML in Javascript function

I am trying to create an email message (when a user clicks a link) whose body is pre-filled with text from an asp:Literal. 我正在尝试创建一封电子邮件(当用户单击链接时),其正文中预先填充了来自asp:Literal的文本。 The HTML is written as follows: HTML编写如下:

<tr>
    <td>
        <img src="../../images/Question.gif" alt="Q" />
        Q:
        <span id="question"><asp:Literal ID="literalQuestion" runat="server"></asp:Literal></span>
    </td>
</tr>
<tr>
    <td>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="../../images/Answer.gif" alt="Q" />
        A:
        <span id="answer"><asp:Literal ID="literalAnswer" runat="server"></asp:Literal></span>
    </td>
</tr>
<tr>
    <td>
        &nbsp;
    </td>
</tr>
<tr>
    <td>
        Click <a href="#" onclick="javaScript:emailWrongInfo()">Here</a> to email.
    </td>
</tr>

I also have a JavaScript function that opens an email message when the user clicks the email link: 我还有一个JavaScript函数,当用户单击电子邮件链接时,它将打开电子邮件:

function emailWrongInfo() {
            window.location="mailto:Test@test.com?Subject=Email%20Notification&Body=Question:%0A" + document.getElementById("question").innerHTML+ "%0A%0AAnswer:%0A" + document.getElementById("answer").innerHTML;
    }

When I click the link, the new email message opens, but the question and answer are not filled in in the body portion. 当我单击链接时,将打开新的电子邮件,但正文中未填写问题和答案。 It seems like the span's text is not being pulled. 似乎跨度的文本没有被提取。

Any help would be greatly appreciated. 任何帮助将不胜感激。

Please try changing: 请尝试更改:

    document.getElementById("question").textContent 

to

    document.getElementById("question").innerHTML

Use HTMLElementObject.innerHTML instead of elementNode.textContent. 使用HTMLElementObject.innerHTML而不是elementNode.textContent。

HTMLElementObject.innerHTML is used to get the inner HTML of an element While textContent property returns or sets the text from the selected element. HTMLElementObject.innerHTML用于获取元素的内部HTML,而textContent属性返回或设置所选元素的文本。

function emailWrongInfo() {
            window.location="mailto:Test@test.com?Subject=Email%20Notification&Body=Question:%0A" + document.getElementById("question").innerHTML+ "%0A%0AAnswer:%0A" + document.getElementById("answer").innerHTML;
    }

Hope, it'll solve your problem. 希望能解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM