无法在Javascript函数中访问span tag's innerHTML

Unable to access span tag's innerHTML in Javascript function

本文关键字:tag innerHTML span 访问 Javascript 函数      更新时间:2023-09-26

我试图创建一个电子邮件消息(当用户点击一个链接),其主体是预填充的文本从asp:Literal。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="answerswer"><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>

我还有一个JavaScript函数,当用户单击电子邮件链接时打开电子邮件消息:

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

当我点击链接时,新的电子邮件消息打开,但是问题和答案没有填写在正文部分。span的文本似乎没有被拉出。

使用HTMLElementObject。

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("answerswer").innerHTML;
    }

希望它能解决你的问题。

请尝试更改:

    document.getElementById("question").textContent 

    document.getElementById("question").innerHTML