【问题标题】:catching on-click event of dynamically allocated id捕获动态分配的 id 的点击事件
【发布时间】:2013-09-26 09:16:06
【问题描述】:

element.find('#id1').on('click', function(){ ... 不起作用。

我正在寻找执行上述操作的代码。

/* 下面的文本/代码是我在更深层次的问题。*/

我可以对$('#id1').on('click', function(){ ... 做同样的事情,问题是我正在使用 ng-repeat 为元素动态分配 id

.set(ng-repeat='item in items').my-directive
  button(id='id{{$index}}') press me

很有意思

element.find('#id1') 有效,但 $('#id1').on('click', .... 无效。

以下是一些我的指令代码

$('button').on('click', function() { alert('hi');} // works

$('#id0').on('click', function() { alert('hi);} // does NOT work

更多

$('button').on('click', function() { 

  element.find('#id1').css('width', 1000); // Also Works

}

这意味着使用“元素”你可以参考 ng-repeat 的 $scope 而你不能使用 jQuery 即 使用 jQuery 的简单 $('#id1')。

【问题讨论】:

    标签: jquery angularjs angularjs-ng-repeat


    【解决方案1】:

    对于动态分配id:

    $('#YourWrapperID').on('click','#id1' , function(){
        alert('test');
    });
    

    【讨论】:

      【解决方案2】:

      我认为您正在寻找 .click 函数。

      $( "#id1" ).click(function() {
      

      在这里阅读: http://api.jquery.com/click/

      【讨论】:

      • 请多阅读几行问题。我面临一个技术问题,用 ng-repeat 解决它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多