简体   繁体   English

如何在JavaScript函数中将“ this”作为参数传递?

[英]How to pass `this` as an argument in javascript function?

I am using javascript in my project. 我在我的项目中使用javascript。

I have on HTML table <table id='idDocList'> and I am doing append some html on this table as below code. 我在HTML table <table id='idDocList'> ,我正在在此表上append一些html,如下代码。 But I want to hide respective <tr> when user click on Delete anchor tag. 但是当用户单击“ Delete锚点标记”时,我想隐藏相应的<tr>

$("#idDocList").append("<tr><td>" + file.name + "</td><td>" + sz + "</td><td><a onclick=deleteDocument(this,'" + file.name + "')> Delete</a></td></tr>");

How can i do this using Jquery? 我如何使用Jquery做到这一点?

The following example does not work 以下示例不起作用

function deleteDocument(CurAnchorTag, fileName) {
    $(CurAnchorTag).closest('tr').hide();
}

I don't want to use ID for <a> tag as I have many Documents. 我不想为<a>标签使用ID ,因为我有很多文档。

As a quick fix, you can use like this, 作为快速解决方案,您可以像这样使用,

$(CurAnchorTag).closest('tr').hide();

Replaced <tr> with tr tr替换<tr>

You can remove the inline function call with jquery like this way, 您可以通过jquery这样删除内联函数调用,

$("#idDocList").on("click", "td a", function() {
    $(this).closest("tr").hide();
    var filename = $(this).closest("td").prev().text();
});

I would suggest you to change your code to: 我建议您将代码更改为:

var newRow = $("<tr><td>" + file.name + "</td><td>" + sz + "</td><td><a href='#'> Delete</a></td></tr>").appendTo("#idDocList");
newRow.find( 'a' ).click( function( e ) {
    e.preventDefault();
    $( this ).closest('<tr>').hide();
});

you can just use 你可以用

$(".delete_link").click(function(){$(this).closest('tr').hide();}

Jquery will use the this of which ever element called it. jQuery将使用此元素调用过的this。 There will be no need for the onclick on the html file. 无需在html文件上进行onclick。

You would better use event delegation and get rid of inline onclick handlers all together: 您最好使用事件委托,并一起摆脱内联onclick处理程序:

$('#idDocList').on('click', '.btn-delete', function() {
    $(this).closest('tr').hide();
    // read filename: $(this).data('filename')
});

And use it with HTML (the sting you append): 并将其与HTML结合使用(附加的字符串):

"<tr><td>" + file.name + "</td><td>" + sz + "</td><td><a class="btn-delete" data-filename='" + file.name + "'>Delete</a></td></tr>"

Note the part: 注意部分:

<a class="btn-delete" data-filename="filename">Delete</a>

You recommend you to use event for a class using the jquery 建议您使用jquery将事件用于类

$("#idDocList").append("<tr><td>" + file.name + "</td><td>" + sz + "</td><td><a class='delete_link'> Delete</a></td></tr>");

The code below will add the event and need to execute always after add a "tr", unless you use a delegate to this 下面的代码将添加事件,并且需要在添加“ tr”之后始终执行,除非您为此使用委托

$(".delete_link").click(function(){ $(this).closest("tr").hide() });

If you don't want to use a class you can use this 如果您不想使用课程,可以使用此课程

$("#idDocList td > a").click(function(){ $(this).closest("tr").hide() });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM