没有ID的javascript查找节点 [英] javascript find node without id

查看:89
本文介绍了没有ID的javascript查找节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我使用的Javascript库的限制,我无法为<div>分配ID.不幸的是,我不知道如何将Ttip(一个Javascript工具提示库)中的Tooltip对象附加到没有id的元素上.我不知道这是否可行,但我正在尝试通过其他方式找到该对象,希望该方法可以使我修改id.

Due to a limitation of the Javascript library I'm using, I can't assign an id to a <div>. Unfortunately, I don't know how to attach a Tooltip object from Tipped, a Javascript tooltip library, to the element without an id. I don't know if this is possible, but I'm trying to find the object via other means which will hopefully allow me to modify the id.

我要查找的元素是工具栏上的按钮,但不是HTML按钮.这是一个<div>,具有CSS样式和Javascript事件,使它看起来和感觉像一个按钮.我知道父母的班级名称,也知道祖父母<div>的ID,但就我所知.问题的一部分是,一旦获得对HTML对象的引用,似乎没有很好的参考来说明如何对HTML对象进行迭代操作.我看过很多这样的例子:

The element I'm looking for is a button on a toolbar, but it's not an HTML button. It's a <div> that has CSS styles and Javascript events assigned to make it look and feel like a button. I know the class name of the parent and I know the id of the grandparent <div>, but that's as much as I know. Part of the issue is that there doesn't seem to be a good reference for how to iteratively operate on HTML objects once you get a reference to them. I've seen plenty of examples like this:

var x = document.getElementsById("asdf")

,但没有后续代码显示如何实际执行任何操作. x中有什么?它有什么方法?我知道innerHTML和innerTEXT,但是我不知道它们是否适用于x,x的子代或???. Chrome控制台有所帮助,但我基本上迷失了.

but no follow-up code showing how to actually do anything. What's in x? What methods does it have? I know of innerHTML and innerTEXT, but I can't figure out if they apply to x, a child of x, or ???. The Chrome console has helped a little bit, but I'm basically lost.

这是我的按钮的相关代码:

This is the relevant code for my button:

如您所见,导出"按钮上没有ID,但是父级具有类名,祖父母具有ID.另一个障碍是祖父母的ID不是静态的.它始终以"dhxtoolbar"开头,页面上只有一个工具栏,但是我无法通过正则表达式搜索找到该工具栏.

As you can see, there is no id on the Export button, but the parent has a class name and the grandparent has an id. The other snag is that the grandparent's id isn't static. It always starts with "dhxtoolbar" and there is only one toolbar on the page, but I haven't been able to make a regex search find the toolbar.

最终,我希望能够将笔尖"工具提示附加到导出"按钮.我认为Tipped需要一个ID,但也许不需要.无论如何,我想了解更多有关如何遍历DOM的信息,并且,作为奖励,请弄清楚如何或是否可以更改实时页面上元素的id.谢谢.

Ultimately, I'd like to be able to attach a Tipped tooltip to the Export button. I think Tipped requires an id, but maybe it doesn't. Regardless, I'd like to understand more about how to iterate through the DOM and, as a bonus, figure out how or if I can change the id of an element on a live page. Thanks.

推荐答案

这就是我试图解释的内容:

This is what I was trying to have explained:

var Obj         = document.getElementsByClassName("classname");
var ObjChildren = Obj[0].getElementsByTagName("tag")
var searchText = "string";

for (var i = 0; i < ObjChildren.length; i < i++) {
    if (ObjChildren[i].innerHTML == searchText) {
        console.log(ObjChildren[i].innerHTML);
    }
}

这篇关于没有ID的javascript查找节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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