繁体   English   中英

为什么当我在页面顶部滚动并单击附加表格的链接时,它会向下滚动

[英]Why when i scroll top my page and click link which append a form it scroll me down

我有一个页面显示活动供稿,每个供稿都带有评论链接,我像这样动态添加评论表:

$("#feedList").on("click", ".comment-link", function(e){
        $(this).parent().append('<form method="post" action="#" class="commentForm"><p><textarea class="comment"></textarea></p><p><input type="submit" value="add" class="send-Comment-Btn" /></p></form>');
        $("textarea.comment").select();
        return false;
    }); 

当我向下滚动页面并单击一些评论链接时,它会按预期显示表单,但是,如果我再次滚动至页面顶部,并且尝试单击评论链接,它也会显示该表单,但会滚动至我之前单击过的最后一个链接的底部页面,有人可以告诉我为什么这种行为和解决方案?

$("textarea.comment").select();

将始终将您带到第一个创建的带有“ comment”类的textarea元素。 也许您必须添加一些ID,以增加每次点击的次数,例如:

var someID = 0;
$("#feedList").on("click", ".comment-link", function(e){
someID += 1;
        $(this).parent().append('<form method="post" action="#" class="commentForm"><p><textarea class="comment" id="selectorForComment' + someID + '"></textarea></p><p><input type="submit" value="add" class="send-Comment-Btn" /></p></form>');
        $("#selectorForComment" + someID).select();
        return false;
    }); 

暂无
暂无

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

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