我如何将文本放在脚本标记的文档中

How could I place text inside a document at the script tag?

本文关键字:文档 脚本 文本      更新时间:2023-09-26

我想知道是否有可能通过使用脚本标签作为参考来编写Javascript文档,而不是使用document.getElementById("id")或类似的东西找到的某些部分。

示例:我有一个表单,当您在输入中输入值并按enter键时,它将文本写入使用先前输入值的文档。

现在,我知道它显然会更容易继续并发送它到一个函数与onSubmit在头部,但这是设置它的格式更容易,如果我可以写出来以这种方式。所以,如果有人知道这是如何实现的,请告诉我,谢谢。

<div style="text-align: center; font-size: 15pt; padding-top: 70px;">
    <form onSubmit="reveal(this.a.value, this.b.value)">
        a = <input name="a"/>
        b = <input name="b"/>
    </form>
    <hr>
    <script>
        function reveal(a, b)
        {
            [math]a + b = c[math]
        }
    </script>
</div>

你可以看到我到目前为止所做的。如果有人能告诉我如何使用脚本标签作为参考编写[math]a + b = c[math]到文档。

尝试结果的澄清: [math]a + b = c[math]代表我想要放置在文档中的文本,截至目前,我没有任何设置来实际编写它,这就是为什么它只是坐在那里。我希望它被放置在脚本标签的位置的文档,周围的函数揭示()。我如何将该文本放在文档中周围脚本标记的位置?

我相信这就是您要做的:找到具有reveal()功能的script标记,然后在script标记之前或之后插入字符串。

如果是这种情况,那么尝试如下:(为了简单起见,使用jQuery)

var tag = $("script").filter(function () {
    return /reveal'(')/.test(this.innerText);     //Find the script tag that has
});                                               // the function "reveal()"
tag.after("[math]a + b = c[math]");               //Insert your text
    - or -
$("<span>[math]a + b = c[math]</span>").replaceAll(tag);  //Replace it


PS:

如果你已经有了一个script标签,那么为什么不这样做呢:

<script>
    function reveal(){
        blah();
    }
    document.write("[math]a + b = c[math]");    //The text will be inserted where
                                                //the script tag is located at
</script>