Javascript 在使用";时,如何让jQuery返回表行元素;这是什么;

Javascript 在使用";时,如何让jQuery返回表行元素;这是什么;,javascript,jquery,html,tablerow,Javascript,Jquery,Html,Tablerow,我正在使用asp.net列表视图控件,并在其中构建一个表元素 <asp:ListView ID="ListView1" runat="server"> <LayoutTemplate> <table> <tr class="header"> <td></td> </tr> <tr

我正在使用asp.net列表视图控件,并在其中构建一个表元素

<asp:ListView ID="ListView1" runat="server">
    <LayoutTemplate>
        <table>
            <tr class="header">
                <td></td>
            </tr>
            <tr id="itemPlaceHolder runat="server">
        </table>
    </LayoutTemplate>
    <ItemTemplate>
        <tr onclick="RowClick();">
    </ItemTemplate>
</asp:ListView>

我的问题是,我的RowClick()函数中的
$(this)
没有返回
tr
元素,而是返回整个窗口。我怎样才能做到这一点?我处理这种情况的方法错了吗?这不可能吗?我在追鬼?谢谢请原谅我的标记,我还没有完全掌握它的窍门。

您需要为处理程序调用传递一个自定义的执行上下文,您可以使用它来完成此操作

<tr onclick="RowClick.call(this);">

使用jQuery?嗯,让我想想

jQuery("tr").click(RowClick);
关于。

这在处理代码方面不是很“jQuery”风格。更“恰当”的方法是这样做:

<tr class="row-click">
或者,如果要保留“RowClick”功能,可以执行以下操作:

$(function(){
    $(".row-click").each(function(){
        $(this).on("click",function(){
            RowClick($(this));
        });
    }
});
function RowClick(element){
    var tds = $(element).children('td');
    //Code goes here...
}

我强烈建议不要用JS调用破坏HTML,jQuery处理得很好:

$(document).ready(function(){

    // create the function
    function RowClick(target){
        var tds = target.children('td');

        // another option :)
        var tds = target.find('td');
    }

    // listen for a <tr> to get clicked within the <table> -> <ItemTemplate> combo and pass the <tr> target to the function
    $('table ItemTemplate').on('click', 'tr', function(){
        RowClick($(this));
    });

});
$(文档).ready(函数(){
//创建函数
函数行单击(目标){
var tds=target.children('td');
//另一种选择:)
var tds=target.find('td');
}
//在->组合框中单击,并将目标传递给函数
$('table ItemTemplate')。在('click','tr',function()上{
行单击($(此));
});
});

这是无效的html。@SLaks我已经能够在您提供的链接中找到所有这些内容,但我从来都不知道今天之前有这个页面。我只是想让您知道,您正在
RowClick()中双重包装一个jQuery对象。
。这可能会导致不同的问题,特别是在应用淡入淡出或CSS转换等效果时。双重包装不会导致任何问题,尽管我希望被证明是错误的,并看到一个工作示例,说明它如何导致问题,但它确实会对性能造成一些影响-。就我个人而言,我更喜欢在这种情况下进行双重包装,这样,如果开发人员稍后看到“元素”,并传入原始dom元素而不是jquery包装的元素,那么该函数将是灵活的。您正在用比必要时更详细的代码来破坏JavaScriptP我真的不认为
ItemTemplate
是呈现HTML的一部分。不确定。。。但是非常肯定。@CookieMonester Idk关于
的事,因为我从来没有做过ASP编程,所以你很可能是对的。尽管我不得不强烈反对你的
搞乱js
语句,因为它的好处远大于缺点。想象一下,如果OP正在创建100个
标记。这是23个字节(`onclick=“RowClick();”`)x100
多余的冗余代码,相当于每页加载2300个额外字节。额外的JS约为86字节。这些行很可能也是动态的,因此服务器每个访问者需要处理的字节数减少了2300个。如果有那么多,那么我建议事件委派将是首选的解决方案,无论是内联的还是JS的。如果是内联的,它们将被压缩到几乎为零。无论哪种方式,一个优点是处理程序可以立即使用,这在某些情况下非常重要,因为在这些情况下,您不能有延迟。不是说内联处理程序无处不在。只是说他们不应该被如此广泛地忽略。根据需要更改选择器。
$(function(){
    $(".row-click").each(function(){
        $(this).on("click",function(){
            RowClick($(this));
        });
    }
});
function RowClick(element){
    var tds = $(element).children('td');
    //Code goes here...
}
$(document).ready(function(){

    // create the function
    function RowClick(target){
        var tds = target.children('td');

        // another option :)
        var tds = target.find('td');
    }

    // listen for a <tr> to get clicked within the <table> -> <ItemTemplate> combo and pass the <tr> target to the function
    $('table ItemTemplate').on('click', 'tr', function(){
        RowClick($(this));
    });

});