Javascript jquery中的触发事件和绑定

Javascript jquery中的触发事件和绑定,javascript,jquery,Javascript,Jquery,我是jQuery新手,一直在玩弄它。这就是我所拥有的: <fieldset id="question-7"> <p> <label>Question 7</label> </p> <p> We need a custom event named "profileUpdated". Bind to this event and make the word "Updated" appear in th

我是jQuery新手,一直在玩弄它。这就是我所拥有的:

<fieldset id="question-7">
  <p>
    <label>Question 7</label>
  </p>
  <p>
    We need a custom event named "profileUpdated". Bind to this event and make the word "Updated" appear in the output div when triggered.
    Trigger the event when this <a href="#">link</a> is clicked.
  </p>
  <p class="output">
  </p>
</fieldset>

我不确定我做错了什么

首先,没有@dfsq所说的被称为链接的事件,但是用于更灵活的事件绑定 您可以使用.on().delegate()

例如:

$(document).ready(function(){
   $('#question-7').on('click',function(){
        alert($(this).text());
   }
}
试试这个:

$(document).ready(function() {
    $("#link").on( "profileUpdated", function( e ) {
        $(".output").html("Updated");
    });

    $("#link").click(function() {
        $( this ).trigger( "profileUpdated" );
    });
});

要回答你做错了什么的问题,我们需要知道你想做什么。没有此类事件“链接”。我试图在单击链接时触发更新的事件配置文件。当我单击要更新的链接以显示在输出分区中时。谢谢为什么需要两个事件处理程序?根据问题中所述的要求,“profileUpdated”事件应在单击“link”锚标记时触发。您只需将
$(.output”).html(“Updated”)
放入
$(“#link”).clik()中即可
ant就是这样,你可能有一个观点库马尔,尽管如果想法是将
profileUpdated
事件与
link
/
点击
事件分开,那么
profileUpdated
在链接上是没有意义的。@hamism同意。但是,根据Nellinel的描述,他有一些要求,需要他在点击链接时触发“profileUpdated”事件。只有在触发“profileUpdated”事件时,文本“Updated”才应添加到输出块中。
$(document).ready(function() {
    $("#link").on( "profileUpdated", function( e ) {
        $(".output").html("Updated");
    });

    $("#link").click(function() {
        $( this ).trigger( "profileUpdated" );
    });
});