【问题标题】:Difficulty onclick selecting by class with jquery (in order to insert html)使用jquery按类选择onclick困难(为了插入html)
【发布时间】:2011-07-03 15:14:31
【问题描述】:

我正在为我认为是一个相当简单的 jquery 函数而苦苦挣扎。我有一个带有“commentreply”类的文本。我正在尝试创建一个 jquery 功能,该功能在用户可以用来回复评论的文本下直接插入一个表单。

由于某种原因,我无法让 jquery 选择“commentreply”类以通过单击操作 DOM。

<script type="text/javascript">
$(document).ready(function(){
$(".commentreply").click(function(){
$(this).html( "the form code would go here" );
}

}
</script>

<span class="commentreply">Reply</span>

这有意义吗?这看起来相当简单,但我一直在做一些小改动,试图让这该死的东西工作大约 3.5 小时。

【问题讨论】:

    标签: jquery class onclick


    【解决方案1】:

    您的事件处理程序(clickready)函数周围缺少右括号:

    $(document).ready(function(){
        $(".commentreply").click(function(){
            $(this).html( "the form code would go here" );
        });
    });
    

    注意最后两行的右括号。另请注意,我在这些行的末尾添加了一个分号 - 它可以在没有它们的情况下工作,就像你拥有的那样,但最好包含它们。除此之外,它应该可以正常工作。

    【讨论】:

    • @bill 您使用 Firebug 或任何其他浏览器开发工具吗?它可以帮助您避免这些简单的语法错误。
    • 是的,我确实使用萤火虫。但老实说,我还没有弄清楚如何将它用于 javascript。我已经使用 php 大约一年了,但我刚刚进入 javascript/jquery 的世界。
    • @James,是的,这是正确的,代码现在可以工作了,谢谢。
    【解决方案2】:

    这对我有用:

    $('#id a').live('click',function(){
      $(this).html( "the form code would go here" );
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-11
      • 2020-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-25
      • 1970-01-01
      相关资源
      最近更新 更多