简体   繁体   English

Javascript,将动态内容写入textarea

[英]Javascript, writing dynamic content to textarea

I am programming a blog in php, for the record I've just started out but Im doing pretty good with this PHP but Javascript I don't really understand. 我正在用php编写博客,以记录我刚刚起步的情况,但是我在使用此PHP方面做得很好,但是我不太了解Javascript。

On my blog I would like people to comment and discuss. 在我的博客上,我希望人们发表评论和讨论。

What I want: 我想要的是:

Every comment has an id that is displayed. 每个评论都有一个显示的ID。

I want to click on it and then the Javascript should place it in the textarea for leaving comments. 我要单击它,然后Javascript应该将其放在文本区域中以留下评论。

Thats all. 就这样。

(This answer uses jQuery.) (此答案使用jQuery。)

// getting a reference to the TEXTAREA element
var $ta = $("#new_comment textarea");

$(".comment").click(function() {
    var text = $(this).find(".text").text(); // depends of the structure
    $ta.val(text);
});

<div class="comment">
    <p class="author"> Peter </p>
    <p class="text"> Good article! </p>
</div>

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

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