文本+变量连接作为 appendChild($variable) 中的字符串与纯 javascript

text+variable concatenation as string inside appendChild($variable) with pure javascript

我知道零 javascript。我通过转换和组合小脚本实现了我的大部分要求。然而,在我下面描述的那一点上,我无法完全解决我的要求。

我也想在常量文本之前打印超链接 ID 的值。 我不使用 jquery。

比如暂时只写“暂时正在回复

但是(假设超链接id = 6)它应该写成“6 正在回复。

我已阅读此 SO 问答,但无法从中受益。 ()

DEMO FOR THE CURRENT STATE @JSFIDDLE

正文标签中的 JS

function reply_comment (id) {
    var LinkId = document.getElementById('parent_id');    
    LinkId.value = id;
    var message = document.createTextNode(" is being replied by the time being.");
    document.getElementById('print_id').appendChild(message);
    document.getElementById("focus_id").focus();
}

HTML

<!-- codes given above -->
<script type="text/javascript">
... ... ...
</script>
<!-- codes given above -->

<!-- when clicked on the hyperlink, store the hyperlink id value (id value is an unsigned integer)  -->
<!-- print the stored id from hyperlink into the value="{unsigned integer id}" part of input ( type:text and id="parent_id" )-->
<!-- focus on comment form's input ( type:text and id="focus_id" )  -->
<!-- print preset message between span tags ( span tag id="print_id"  ) -->


<a class="rep"  href="#focus_id" onclick="reply_comment(this.id);" id="6" rel="nofollow">reply comment</a>
<p>filling words</p><p>filling words</p><p>filling words</p><p>filling words</p><p>filling words</p><p>filling words</p><p>filling words</p><p>filling words</p><p>filling words</p><p>filling words</p><p>filling words</p><p>filling words</p><p>filling words</p><p>filling words</p><p>filling words</p><p>filling words</p><p>filling words</p><p>filling words</p><p>filling words</p><p>filling words</p><p>filling words</p><p>filling words</p><p>filling words</p><p>filling words</p><p>filling words</p><p>filling words</p><p>filling words</p><p>filling words</p>
<span id="print_id"></span>
<form>
    <label for="focus_id">Focus the cursor below</label>
    <input  type="text" id="focus_id" name="focus_id">
    <label for="parent_id">Paste parent_id below</label>
    <input  type="text" id="parent_id" name="parent_id" value="">
</form>

对我来说,您似乎从未将 LinkId.value = id 添加到您的 message

function reply_comment (id) {
    var LinkId = document.getElementById('parent_id');    
    LinkId.value = id;
    var message = document.createTextNode(id + " is being replied by the time being.");
    document.getElementById('print_id').appendChild(message);
    document.getElementById("focus_id").focus();
}

Fiddle: http://jsfiddle.net/fiddle_me_this/nc43vypp/5/

我所做的只是在 " is being replied by the time being."

中添加 id +

为了串联,您只需使用 + 运算符。你可以连接很多东西。示例:var string = "MY " + "CONCATENATED " + " STRING"; 将输出 "MY CONCATENATED STRING"。等等。