简体   繁体   English

单击TD中的“ OnClick”功能时,使用jQuery获得最近的TR吗?

[英]Getting Closest TR with jQuery when clicking on an “OnClick” function inside a TD?

Is this possible? 这可能吗? Is the fact I am using an inline onclick function deterring from getting the TR row? 我使用内联onclick函数阻止获取TR行的事实吗?

My Table Row looks like this: 我的表格行看起来像这样:

<tr role="row" class="odd"><td><a class="masterskutd" id="mastersku_FL538-J"
      onclick="editMasterSKU('FL538-J');return false;">FL538-J</a></tr>

In an external JS file functions.js I am trying things such as this 在外部JS文件functions.js我正在尝试这样的事情

function editMasterSKU(SKU) {
    var tr = $(this).closest('tr');
    $(tr).addClass('clicked');
    // var tr = $(this).parents('tr');
    console.log(tr);

Nothing I do seems to catch the TR that the function was clicked upon, the console.log only brings back w.fn.init(0) (not entirely sure what this means) with a length of 0, and it is not adding the class to the row. 我似乎什么也没捕捉到单击该函数的TR, console.log仅带回长度为0的w.fn.init(0) (并不完全确定这是什么意思),并且未添加上课。

How can I get the TR in a jQuery variable when clicking on my onclick function inside the td / a ? td / a单击onclick函数时,如何在jQuery变量中获取TR?

The issue is because functions called from inline event attributes do not run under the scope of the element which raised the event. 问题是因为从内联事件属性调用的函数不在引发事件的元素的范围内运行。 They instead run under the scope of the window , hence this is not what you expect it to be. 它们而是在window范围内运行,因此this不是您期望的那样。

To fix this you can pass this as an argument to the function from the onclick : 要解决此问题,您可以将this作为参数从onclick传递给函数:

 function editMasterSKU(a, SKU) { var tr = $(a).closest('tr'); $(tr).addClass('clicked'); } 
 .clicked { color: #C00; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <tr role="row" class="odd"> <td> <a class="masterskutd" id="mastersku_FL538-J" onclick="editMasterSKU(this, 'FL538-J');return false;">FL538-J</a> </td> </tr> </table> 

However it's worth noting that you should not be using inline event attributes at all. 但是,值得注意的是,您根本不应该使用内联事件属性。 Use unobtrusive event handlers instead. 改用不显眼的事件处理程序。 As you're already including jQuery in the page it can be done like this: 由于您已经在页面中包含了jQuery,因此可以这样完成:

 jQuery(function($) { $('.masterskutd').on('click', function(e) { e.preventDefault(); var $tr = $(this).closest('tr'); $tr.addClass('clicked'); console.log($(this).data('sku')); }); }) 
 .clicked a { color: #C00; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <tr role="row" class="odd"> <td> <a class="masterskutd" id="mastersku_FL538-J" data-SKU="FL538-J" href="#">FL538-J</a> </td> </tr> </table> 

Note the use of the data attribute to store the custom meta data in the element itself, and also the addition of the href attribute, which is required for the HTML to be valid. 请注意,使用data属性将自定义元数据存储在元素本身中,还添加了href属性,这是HTML有效的必需属性。

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

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