Javascript jquery onclick事件不';我不为林克工作

Javascript jquery onclick事件不';我不为林克工作,javascript,jquery,jsp,Javascript,Jquery,Jsp,就在之前,我的jsp页面顶部有一个脚本。除了的onclick事件外,其他一切都正常工作。这是我的代码: $(文档).ready(函数(){ $.ajax({ url:'分页', 键入:“获取” }).成功(功能(数据){ $(“#分页”).empty(); $.each(数据、函数(索引、用户){ $('#分页')。附加($('') .append($('').append($('').attr({ 'onclick':'GoToProfile('+user.username+')' }).t

就在
之前,我的jsp页面顶部有一个脚本。除了
的onclick事件外,其他一切都正常工作。这是我的代码:

$(文档).ready(函数(){
$.ajax({
url:'分页',
键入:“获取”
}).成功(功能(数据){
$(“#分页”).empty();
$.each(数据、函数(索引、用户){
$('#分页')。附加($('')
.append($('').append($('').attr({
'onclick':'GoToProfile('+user.username+')'
}).text(user.username)))
.append($('').text(user.email)))
});
$(“#销售额”)。数据表({
“sPaginationType”:“完整编号”,
“bJQueryUI”:没错,
});
});
});
函数GoToProfile(用户){
window.location.href=“/profile/”+用户;
}

我建议使用这种方法,尽管其余的代码编写得不是很好。我没有测试它,但它应该可以工作

$(document).ready(function () {
  function GoToProfile(user) {
    window.location.href = "/profile/" + user;
  }
  $.ajax({
    url: 'paging',
    type: 'GET'
  }).success(function (data) {
    $('#pagination').empty();
    $.each(data, function (index, user) {
      $('#pagination').append($('<tr>')
        .append($('<td>').append($(`<a data-username="${user.username}">`).text(user.username)))
        .append($('<td>').text(user.email)))
      });

      $("#sales").dataTable({
        "sPaginationType": "full_numbers",
        "bJQueryUI": true,
      });
   });
  $("#pagination").on("click", "a", function(){
    const $this = $(this);
    GoToProfile($this.attr(data-username));
  });
});
$(文档).ready(函数(){
函数GoToProfile(用户){
window.location.href=“/profile/”+用户;
}
$.ajax({
url:'分页',
键入:“获取”
}).成功(功能(数据){
$(“#分页”).empty();
$.each(数据、函数(索引、用户){
$('#分页')。附加($('')
.append($('').append($(`').text(user.username)))
.append($('').text(user.email)))
});
$(“#销售额”)。数据表({
“sPaginationType”:“完整编号”,
“bJQueryUI”:没错,
});
});
$(“#分页”)。在(“单击”,“a”,函数()上){
const$this=$(this);
GoToProfile($this.attr(数据用户名));
});
});

我建议使用这种方法,尽管代码的其余部分编写得不是很好。我没有测试它,但它应该可以工作

$(document).ready(function () {
  function GoToProfile(user) {
    window.location.href = "/profile/" + user;
  }
  $.ajax({
    url: 'paging',
    type: 'GET'
  }).success(function (data) {
    $('#pagination').empty();
    $.each(data, function (index, user) {
      $('#pagination').append($('<tr>')
        .append($('<td>').append($(`<a data-username="${user.username}">`).text(user.username)))
        .append($('<td>').text(user.email)))
      });

      $("#sales").dataTable({
        "sPaginationType": "full_numbers",
        "bJQueryUI": true,
      });
   });
  $("#pagination").on("click", "a", function(){
    const $this = $(this);
    GoToProfile($this.attr(data-username));
  });
});
$(文档).ready(函数(){
函数GoToProfile(用户){
window.location.href=“/profile/”+用户;
}
$.ajax({
url:'分页',
键入:“获取”
}).成功(功能(数据){
$(“#分页”).empty();
$.each(数据、函数(索引、用户){
$('#分页')。附加($('')
.append($('').append($(`').text(user.username)))
.append($('').text(user.email)))
});
$(“#销售额”)。数据表({
“sPaginationType”:“完整编号”,
“bJQueryUI”:没错,
});
});
$(“#分页”)。在(“单击”,“a”,函数()上){
const$this=$(this);
GoToProfile($this.attr(数据用户名));
});
});
正如斯科特·马库斯所说:

attr({'onclick': 'GoToProfile(' + user.username + ')'}) 
应改为

on('click', function(){GoToProfile(user.username)})
令人困惑的是,我在项目的其他地方使用了完全相同的语法来创建链接,它们工作得非常好。我只是不知道为什么它在这里不起作用

正如斯科特·马库斯所说:

attr({'onclick': 'GoToProfile(' + user.username + ')'}) 
应改为

on('click', function(){GoToProfile(user.username)})

令人困惑的是,我在项目的其他地方使用了完全相同的语法来创建链接,它们工作得非常好。我只是不知道为什么它在这里不起作用

您可能在浏览器控制台中遇到语法错误。修复语法错误后,请将:
.attr({'onclick':'GoToProfile('+user.username+')')}
更改为:
.on('click',function(){GoToProfile(ser.username);})
您可能在浏览器控制台中遇到语法错误。修复语法错误后,将:
.attr({'onclick':'GoToProfile('+user.username+')}
更改为:
.on('click',function(){GoToProfile(ser.username);})