首页 > 解决方案 > 如何将元素对象作为参数发送到没有 ID 的 javascript 函数

问题描述

在 ClickedRow 函数中,我想使用正在单击的“a”。所以我想将它作为参数接收。

<td ...... >
<span class="......>
  <span onmousedown="event.cancelBubble = true">
      <a class="GridLinkRenderer" href="javascript:ClickedRow(this)"  onclicklink="javascript:ClickedRow(this)" urlText="XXXX">

<td ......
<span class="......>
  <span onmousedown="event.cancelBubble = true">
      <a class="GridLinkRenderer" href="javascript:ClickedRow(this)"  onclicklink="javascript:ClickedRow(this)" urlText="XXXXX">

基于 clicked<a ....>我想通过函数 ClickedRow(this)隐藏/显示它(或<a class= "GridLinkRenderer"在 other中显示/隐藏下一个)。<td ...>我该怎么做 ?

我试图发送点击的 $(row).next().eq(0).tagName 和row.style.display = 'none',它说它是“未定义的”。

function ClickedRow(row){

$(row).next().eq(0).hide();
$(row).hide();


}

标签: javascriptjqueryhtmlthishref

解决方案


所以复制了你的样本并想出了这个。;) 试试这个.. 并确保你明白这里发生了什么。

$(() => {
            $("body").on("click",".GridLinkRenderer", (e) => {
                e.preventDefault();
                //works on the first link.. stil misses a few check.. 
                //for example do we have the next td.. at all.. 
                console.log($(e.currentTarget).closest('td').next('td').find('.GridLinkRenderer'));
            })
        });

推荐阅读