JQuery - 使用DOMNodeInserted元素 [英] JQuery - use element with DOMNodeInserted

查看:1517
本文介绍了JQuery - 使用DOMNodeInserted元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以肯定这个问题很简单,但我无法弄明白。

For sure this question is very easy, but I just cannot figure this out.

我正在使用DOMNodeInserted事件来检测何时插入新元素。

I'm using DOMNodeInserted event to detect when a new element is inserted.

我不知道如何使用当前元素,例如获取它的父ID。

I don't know how to use the current element, for example to get it's parent id.

现在我有这样的函数:

document.addEventListener("DOMNodeInserted", function(event){
  var element = event.target;

  if (element.tagName == 'DIV') {
    if (element.id == 'ndiv_3-1VTSTHR') {
     alert($('#ndiv_3-1VTSTHR').parent().get(0).tagName);
    }
  }
});

这有效,但它会给我ndiv_3-1VTSTHR元素的父级。我想知道任何元素的父级,使用JQuery。

This works, but it will give me the parent to ndiv_3-1VTSTHR element. I want to know the parent to any element, using JQuery.

我试过

alert($(this).parent().get(0).tagName);

但没有运气。

推荐答案

您可能需要在 element.target 周围初始化一个jQuery对象。尝试:

You probably need to initialize a jQuery object around element.target. Try:

document.addEventListener("DOMNodeInserted", function(event) {
    alert($(event.target).parent()[0].tagName);
});

这篇关于JQuery - 使用DOMNodeInserted元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆