在普通 JS 中获取单击元素的父节点

发布于 2025-01-04 19:27:08 字数 1317 浏览 3 评论 0原文

我需要在纯 JS 中获取单击元素的父节点(没有 jQuery 或其他框架) 我目前正在使用 document.getElementById("item_click") 但我想将 id="item_click" 更改为 class="item_click" 这样我就可以使用多个框。 我只是不知道如何将 this 集成到脚本中

Here's a Fiddle <<<玩它

HTML

<div class="item">
    <div  class="item-tester" >
        <div class="item-icon"></div>
        <div class="item-title">Item Title</div>
    </div>
    <div id="item_click" onmousedown="new_class(event)" onmouseup="revert_class(event)" onmouseout="revert_class(event)"></div>
</div>

JS

function new_class(event) {
    wClick = document.getElementById("item_click");
    wTile = wClick.parentNode;
    wTile.className = wTile.className + " added-class";
}
function revert_class(event) {
    wTile.className = "item";
}
​

我想更改

wClick = document.getElementById("item_click");
wTile = wClick.parentNode;

为类似

wClick = this;
wTile = wClick.parentNode;

我知道如何在 jQuery 中执行此操作的内容,但它在纯 JS 中不起作用,因为 this 将是 window (我想想)

顺便说一句。 我需要(事件),因为这只是我正在使用的整个代码的精简版。

I need to get the parentNode of a clicked element in plain JS (no jQuery or other frameworks)
I am currently using document.getElementById("item_click")
but I want to change id="item_click" to class="item_click" so I can use multiple boxes.
I just don't know how to integrate this in the script

Here's a Fiddle <<< play with it

HTML

<div class="item">
    <div  class="item-tester" >
        <div class="item-icon"></div>
        <div class="item-title">Item Title</div>
    </div>
    <div id="item_click" onmousedown="new_class(event)" onmouseup="revert_class(event)" onmouseout="revert_class(event)"></div>
</div>

JS

function new_class(event) {
    wClick = document.getElementById("item_click");
    wTile = wClick.parentNode;
    wTile.className = wTile.className + " added-class";
}
function revert_class(event) {
    wTile.className = "item";
}
​

I want to change

wClick = document.getElementById("item_click");
wTile = wClick.parentNode;

to something like

wClick = this;
wTile = wClick.parentNode;

I know how to do this in jQuery but it will not work in plain JS as this would be the window (I think)

BTW.
I need the (event) since this is just a stripdown of the entire code I'm using.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情绪少女 2025-01-11 19:27:08
function new_class(event) {
    wTile = event.target.parentNode;
    wTile.className = wTile.className + " added-class";
}
function new_class(event) {
    wTile = event.target.parentNode;
    wTile.className = wTile.className + " added-class";
}
幻梦 2025-01-11 19:27:08

您可以只使用 event.target.parentNode甚至不需要在函数中传递 event):

function new_class() {
    var parent = event.target.parentNode;
    // Do whatever...
}

You can just use event.target.parentNode (Don't need to even pass the event in the function):

function new_class() {
    var parent = event.target.parentNode;
    // Do whatever...
}
奢欲 2025-01-11 19:27:08

“我只是不知道如何将这个集成到脚本中”

使用 .call() 调用处理程序来设置其 this 值为具有处理程序的元素...

<div id="item_click" onmousedown="new_class.call(this,event)" ...>

function new_class(event) {
    var wTile = this.parentNode;
    wTile.className = wTile.className + " added-class";
}

"I just don't know how to integrate this in the script"

Use .call() to invoke the handler to set its this value to the element that has the handler...

<div id="item_click" onmousedown="new_class.call(this,event)" ...>

function new_class(event) {
    var wTile = this.parentNode;
    wTile.className = wTile.className + " added-class";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文