Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有办法只将jQuery文本附加到同一个div_Jquery_Html - Fatal编程技术网

有没有办法只将jQuery文本附加到同一个div

有没有办法只将jQuery文本附加到同一个div,jquery,html,Jquery,Html,我正在制作一个评论系统。我有一个带笑脸的div。每次单击图像时,图像文本将附加到文本区域。但问题是,每次我点击一个笑脸图像,它就会将文本附加到页面上的所有文本区域,因为所有的teaxtareas都有相同的类。我也尝试给textarea一个id,但是它只会将文本附加到第一个teatarea 以下是我的HTML代码: <a href="javascript:void(0)" onclick="appendpostid(' :3 ')"> <img src="emotions

我正在制作一个评论系统。我有一个带笑脸的div。每次单击图像时,图像文本将附加到文本区域。但问题是,每次我点击一个笑脸图像,它就会将文本附加到页面上的所有文本区域,因为所有的teaxtareas都有相同的类。我也尝试给textarea一个id,但是它只会将文本附加到第一个teatarea

以下是我的HTML代码:

<a href="javascript:void(0)" onclick="appendpostid(' :3 ')">
    <img src="emotions/fb/colonthree.gif" class="smiley">
</a>

<textarea class="comment" name="comment" required></textarea>

以及jQuery脚本:

<script>
    function appendpostid( postid ) {
        $('.comment').val($('.comment').val() + postid);
    }
</script>  

函数appendpostid(postid){
$('.comment').val($('.comment').val()+postid);
}

从声音上看,您希望有多组这样的按钮和文本区域。在本例中,一个选项是使用方法为按钮选择textarea,该方法选择DOM中的下一个元素。您可以使用
.comment
类作为选择器来实现这一目标

在本例中,我还稍微修改了您的HTML,将post ID作为数据属性放入其中,并应用了一个类,您可以使用jQuery绑定click事件:

<a href="#" data-post-id=" :3 " class="appender">
    <img src="emotions/fb/colonthree.gif" class="smiley"/>
</a>
有多套。试试这个

<a href="javascript:void(0)" onclick="appendpostid(e, this,' :3 ')">
    <img src="emotions/fb/colonthree.gif" class="smiley">
</a>

<textarea class="comment" name="comment" required></textarea>

和jquery

<script>
    function appendpostid(e, elem, postid ) {
        e.preventDefault();
        var textElem = $(elem).next('.comment');
        textElem.val(textElem.val() + postid);
    }
</script>  

函数appendpostid(e、elem、postid){
e、 预防默认值();
var textElem=$(elem).next('.comment');
textElem.val(textElem.val()+postid);
}

您必须在文本区域中添加一个id,然后从那里看起来很明显。。。
<script>
    function appendpostid(e, elem, postid ) {
        e.preventDefault();
        var textElem = $(elem).next('.comment');
        textElem.val(textElem.val() + postid);
    }
</script>