简体   繁体   English

文本+变量串联为纯JavaScript的appendChild($ variable)中的字符串

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

I know zero javascript. 我知道JavaScript为零。 I achieved most of my requirements by converting & combining little scripts. 我通过转换和组合小脚本达到了我的大部分要求。 however at the point I described below I couldn't solve my requirement totally. 但是,在我下面描述的那一点上,我无法完全解决我的要求。

I want to print the value of the hyperlink id before my constant text also. 我也想在我的常量文本之前打印超链接id的值。 I don't use jquery. 我不使用jQuery。

For example for the time being it's only writing " is being replied by the time being. " 例如,暂时只写“ 正在被答复 ”。

However (assume hyperlink id = 6) it should write " 6 is being replied by the time being. " 但是(假设超链接id = 6)它应该写为“ 6 正在被暂时答复。

I've read this SO Q&A but I couldn't benefit from it. 我已经阅读过此类问答,但我无法从中受益。 ( JS: How to concatenate variables inside appendChild()? ) JS:如何在appendChild()中连接变量?

DEMO FOR THE CURRENT STATE @JSFIDDLE 当前状态的演示@JSFIDDLE

JS in body tags 身体标签中的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 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>

to me it looks like you never added the LinkId.value = id to your message : 对我来说,您似乎从未将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/ 小提琴: http : //jsfiddle.net/fiddle_me_this/nc43vypp/5/

All i did was add id + in from of " is being replied by the time being." 我所做的只是在" is being replied by the time being."添加id + " is being replied by the time being."

In order to concatenate you just use the + operator. 为了连接,只需使用+运算符。 You can concat lots of things. 您可以合并很多东西。 Example: var string = "MY " + "CONCATENATED " + " STRING"; 示例: var string = "MY " + "CONCATENATED " + " STRING"; will output "MY CONCATENATED STRING" . 将输出"MY CONCATENATED STRING" And so on. 等等。

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

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