在jquery中触发事件和绑定

时间:2014-11-02 16:04:46

标签: 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 the output div when triggered.
    Trigger the event when this <a href="#">link</a> is clicked.
  </p>
  <p class="output">
  </p>
</fieldset>

我的jQuery是:

function profileUpdated() {
  $( "#question-7" ).bind( "link", function() {
    alert( $( this ).text('Updated') );
  });
});

我不确定我做错了什么。

2 个答案:

答案 0 :(得分:1)

首先,没有事件称为&#34;链接&#34; ,如@dfsq所说,但更灵活的事件绑定 你可以使用 .on() .delegate()

示例:

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

答案 1 :(得分:0)

试试这个:

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

    $("#link").click(function() {
        $( this ).trigger( "profileUpdated" );
    });
});
相关问题