使用 javascript 查找动态添加的标签

发布于 2024-08-27 08:01:49 字数 433 浏览 5 评论 0原文

我正在尝试了解如何找到在表中动态添加的标签,如下所示:

ParentTag= document.getElementById('parentdiv');
var newTag = document.createElement('div');
newTag.innerHTML="<span class="ImNew"></span>" 
ParentTag.appendChild(newTag);

How will I be able to find that tag in javascript, not倾向使用jquery,所以没有live 请提出建议。尝试在严格的 javascript 中找到该新标签。

我指的标签是span标签

I am trying to see how to find a tag that was added dynamically within a table as so:

ParentTag= document.getElementById('parentdiv');
var newTag = document.createElement('div');
newTag.innerHTML="<span class="ImNew"></span>" 
ParentTag.appendChild(newTag);

How will I be able to find that tag in javascript, not leaning towards using jquery so no live recommendations please.. Trying to find that new tag in strictly javascript.

The tag I am referring to is the span tag

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

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

发布评论

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

评论(3

反话 2024-09-03 08:01:49

您可以在创建新标签时为其指定一个 ID:

ParentTag= document.getElementById('parentdiv');
var newTag = document.createElement('div');
newTag.setAttribute('id','myNewID');
newTag.innerHTML="<span class="ImNew"></span>" 
ParentTag.appendChild(newTag);

然后,只需使用该 ID 即可找到它:

var newTag = document.getElementById('myNewID');

You could give your new tag an ID when you create it:

ParentTag= document.getElementById('parentdiv');
var newTag = document.createElement('div');
newTag.setAttribute('id','myNewID');
newTag.innerHTML="<span class="ImNew"></span>" 
ParentTag.appendChild(newTag);

Then later, just use that ID to find it:

var newTag = document.getElementById('myNewID');
剑心龙吟 2024-09-03 08:01:49

这取决于元素中还存在哪些其他元素。例如,您可以获取元素中的所有 span 标记,并过滤掉具有特定类名的标记:

var parentTag = document.getElementById('parentdiv');
var spans = parentTag.getElementsByTagname('SPAN');
var filtered = [];
for (var i=0; i<spans.length; i++) {
  if (spans[i].className === 'ImNew') filtered.push(spans[i]);
}

如果元素中只有一个具有该类名的 span 标记,则 filtered 数组现在包含一个对该元素的引用。

That depends on what other elements exist in the element. You can for example get all span tags in the element and filter out the ones with a specific class name:

var parentTag = document.getElementById('parentdiv');
var spans = parentTag.getElementsByTagname('SPAN');
var filtered = [];
for (var i=0; i<spans.length; i++) {
  if (spans[i].className === 'ImNew') filtered.push(spans[i]);
}

If there is only one span tag with that class name in the element, the filtered array now cotnains a reference to that element.

别想她 2024-09-03 08:01:49

正如其他人所提到的,如果您稍后需要它,为什么不直接跟踪它或给它一个 id 呢?

但无论如何,这就是您可以进行手动搜索的方式(在将新项目添加到末尾时从后到前进行搜索)。

var parent = document.getElementById("parentdiv")
for (var i = parent.childNodes.length - 1; i >= 0; i--)
{
    var el = parent.childNodes[i];
    if (el.nodeType == 1 &&
        el.nodeName.toLowerCase() == "div" &&
        el.firstChild &&
        el.firstChild.nodeName.toLowerCase() == "span" &&
        el.firstChild.className = "ImNew")
    {
        // el.firstChild is what you want
    }
}

As others have mentioned, why not just keep track of it or give it an id if you're going to need it later?

But anyway, this is how you could do a manual search (searching from back to front as you're adding the new items to the end).

var parent = document.getElementById("parentdiv")
for (var i = parent.childNodes.length - 1; i >= 0; i--)
{
    var el = parent.childNodes[i];
    if (el.nodeType == 1 &&
        el.nodeName.toLowerCase() == "div" &&
        el.firstChild &&
        el.firstChild.nodeName.toLowerCase() == "span" &&
        el.firstChild.className = "ImNew")
    {
        // el.firstChild is what you want
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文