-2

我的程序中有这段代码,但是该事件没有从 Codepen 中的 JS 执行任何操作,但是当我从 Debug View 将其粘贴到 javascript 控制台中时,它可以工作。

$('.peg-row--selected').children().click(function(e) {
  console.log("hi");
  $(e.target).attr('data-peg',$('.peg-selector--selected').attr('data-peg'));
});
4

1 回答 1

1

在绑定事件之前确保你的元素存在,试试这个:

$(document).ready(function(){
  $('.peg-row--selected').children().click(function(e) {
    console.log("hi");
    $(e.target).attr('data-peg',$('.peg-selector--selected').attr('data-peg'));
  });
});
于 2017-01-25T15:17:47.670 回答