简体   繁体   English

HTML DOM元素名称作为字符串

[英]HTML DOM element name as a string

Suppose I have the following HTML snippet: 假设我有以下HTML代码段:

<input type="text" id="myinput" />

Now I want to get that DOM element using JavaScript: 现在,我想使用JavaScript获取该DOM元素:

var element = document.getElementById("myinput");

Works fine, no problem so far. 效果很好,到目前为止没有问题。

But when I print it inside an alert box using alert(element); 但是当我使用alert(element);在一个警告框中打印它时alert(element); , it displays object HTMLInputElement . ,它显示object HTMLInputElement
Is there a way to get that element name (HTMLInputElement) as a string? 有没有一种方法来获取该元素名称(HTMLInputElement)作为字符串?

(Notice that when saying "element name" I do not mean the name attribute of an element, but the name how it is displayed when using alert() for example, as described above. (请注意,当说 “元素名称”时, 我并不是指 元素 name 属性,而是名称,例如,如上所述,当使用alert()时如何显示该名称。

In some browsers, such as Firefox (and Chrome, potentially others) you can do: 在某些浏览器中, 例如Firefox (和Chrome,可能还有其他浏览器),您可以执行以下操作:

element.constructor.name; // => "HTMLInputElement"

But in general it's a bit more complicated , perhaps not even totally reliable. 但是总的来说,它有点复杂 ,甚至可能不是完全可靠的。 The easiest way might be as such: 最简单的方法可能是这样的:

function getClassName(o) {
  // TODO: a better regex for all browsers...
  var m = (o).toString().match(/\[object (.*?)\]/);
  return (m) ? m[1] : typeof o;
}
getClassName(element); // => "HTMLInputElement"
getClassName(123); // => "number"

[Edit] [编辑]

Or, using the "nodeName" attribute , you could write a utility function which should be generally much more reliable: 或者,使用“ nodeName”属性 ,您可以编写一个实用程序函数,该函数通常应该更加可靠:

function getHtmlElementClassName(htmlElement) {
  var n = htmlElement.nodeName;
  if (n.matches(/^H(\d)$/)) {
    return "HTMLHeadingElement";
  } else if (/* other exceptional cases? */) {
    // ...
  } else {
    return "HTML" + n.charAt(0) + n.substr(1).toLowerCase() + "Element";
  }
}

(Thanks @Esailija for the smarter implementation, @Alohci for pointing out exceptional cases.) (感谢@Esailija的更聪明的实现,感谢@Alohci指出特殊情况。)

When passing an object to the alert() function, it implicitly calls .toString() on that object in order to get the text for the alert. 将对象传递给alert()函数时,它将隐式调用该对象上的.toString()以获得警报的文本。 You could do something like: 您可以执行以下操作:

var element = document.getElementById("myInput");
var string = element.toString(); // this will return 'object HTMLInputElement'

then work with the string variable to get only the HTMLInputElement part. 然后使用string变量仅获取HTMLInputElement部分。

document.getElementById returns the HTML element as an object. document.getElementById将HTML元素作为对象返回。 Simply get the attribute of the object you want to display in the alert instead (eg, alert(element.getAttribute('ID')); ). 只需获取要显示在警报中的对象的属性即可(例如alert(element.getAttribute('ID')); )。 Alternatively, if you want '[object HTMLInputElement]' displayed in the alert, simply call the toString() method on the object in the alert (eg, alert(element.toString()); ). 或者,如果要在警报中显示“ [object HTMLInputElement]”,只需在警报中的对象上调用toString()方法(例如, alert(element.toString()); )。

Hope this helps, 希望这可以帮助,

Pete 皮特

如果我有正确的问题,则应尝试document.getElementById("myinput").toString()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM