【问题标题】:jQuery Doesn't Return Cell ContentjQuery 不返回单元格内容
【发布时间】:2013-07-08 13:12:48
【问题描述】:

有人能解释一下为什么下面的方法不起作用,因为它没有返回包装在标签中的单元格的内容吗?我将它与 DataTables 插件结合使用。我基本上想在单元格的当前内容周围加上一个链接。

$('table#example tbody td:nth-child(1), table#example tbody td:nth-child(2)').html('<a href="view-aae.asp?id=">[' + $(this).text() + ']</a>').click(function () {
    $("#thedialog").attr('src', $(this).next('.dialog').attr("href"));
    $("#somediv").dialog({
         width: 800,
         height: 600,
         modal: true,
         close: function () {
              $("#thedialog").attr('src', "about:blank");
         }
     });
     return false
 });

【问题讨论】:

  • 我猜是因为this 不是你想的那样。

标签: jquery jquery-plugins datatables


【解决方案1】:

问题是this 不是你想的那样。您似乎认为this 在构造.html() 函数的参数时将是具有其内容集的特定元素,但事实并非如此——它是别的东西(尽管这完全取决于代码的上下文)。试试这个:

$('table#example tbody td:nth-child(1), table#example tbody td:nth-child(2)').html(function() {
    return '<a href="view-aae.asp?id=">[' + $(this).text() + ']</a>';
});

这样this确实指的是当前具有其内容集的元素。

【讨论】:

    【解决方案2】:

    this 不是这样工作的。

    在您的情况下,$(this) 可能是窗口,尝试将您的代码包装在 .each() 中,它可能会工作:

    $('table#example tbody td:nth-child(1), table#example tbody td:nth-child(2)').each(function(){
        $(this).html('<a href="view-aae.asp?id=">[' + $(this).text() + ']</a>').click(function () {
            $("#thedialog").attr('src', $(this).next('.dialog').attr("href"));
            $("#somediv").dialog({
                width: 800,
                height: 600,
                modal: true,
                close: function () {
                    $("#thedialog").attr('src', "about:blank");
                }
            });
            return false
        });
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-13
      • 1970-01-01
      • 2023-03-16
      • 2016-06-07
      相关资源
      最近更新 更多