简体   繁体   English

如何知道HTML中标记元素的所有属性和属性的值

[英]How to know values of all attributes and properties of a tag element in HTML

I want to see all values of attributes of any tag in HTML. 我想查看HTML中任何标签的属性的所有值。 Even if they are set in the tag or not in HTML. 即使在标记中设置了它们,也没有在HTML中设置它们。 For example: 例如:

<div style="background-color:#000000;">
<p>abc</p>
</div>

I want to know how I can find the background color of <p> . 我想知道如何找到<p>的背景色。 The reason is because there can be many ways to set a value of an attribute in a tag. 原因是因为可以通过多种方法在标签中设置属性的值。 For example: the value can be set in an external css file, javascript file, could be set by a parent tag, could be set in css in same page etc. But is always difficult to track to set value such as font size by reading whole code and opening its all files referenced in HTML code. 例如:该值可以在外部CSS文件,JavaScript文件中设置,可以由父标记设置,可以在同一页面的CSS中设置等。但是,始终很难通过阅读来跟踪设置值(例如字体大小)完整的代码,然后打开HTML代码中引用的所有文件。 I wanted to know value of font of a text enclosed in a <p> but it is very difficult to search on whole page and its associated .js and .css file to search to find out from where the <p> tag is picking its font values. 我想知道包含在<p>中的文本的字体值,但是很难在整个页面及其关联的.js和.css文件中进行搜索,以查找<p>标记从何处选择其内容。字体值。 So, I wanted to know that if I select the text in a browser then is there any way possible to show all the attributes and its values of selected text. 因此,我想知道,如果我在浏览器中选择了文本,那么有没有办法显示所选文本的所有属性及其值。 Just like Visual Studio, Netbeans, Eclipse IDE etc. show all properties of any drag-and-drop object in the design interface. 就像Visual Studio,Netbeans,Eclipse IDE等一样,它在设计界面中显示任何拖放对象的所有属性。

That is exactly what Firebug is for. 那正是Firebug的目的。 I prefer to work with Chrome Inspector, but it is very similar. 我更喜欢使用Chrome Inspector,但是它非常相似。

  • right click on any element you want to 'investigate' 右键单击您要“调查”的任何元素
  • choose 'inspect element' from the context menu 从上下文菜单中选择“检查元素”

Something like this pops up: 这样的事情弹出: chrome inspector的屏幕截图

  • In the bottom right corner you can see all the styles that are applied to this element. 在右下角,您可以看到应用于此元素的所有样式。
    • element.style are the 'inline' styles, that are applied directly in the style attribute of the element element.style是“内联”样式,直接应用于元素的style属性中
    • all the other selectors are comming from your stylesheets. 所有其他选择器都来自您的样式表。 Right next to the selector are the name of the file and the line number this selector is on (in the screenshot it is a minified stylesheet, so the line number does not make much sense) 选择器旁边是文件名和选择器所在的行号(在屏幕截图中是缩小的样式表,因此行号没有太大意义)
    • when hovering over each style declaration, you can see a checkbox next to it. 将鼠标悬停在每个样式声明上时,您可以在其旁边看到一个复选框。 Unchecking this wil disable the style. 取消选中此选项将禁用样式。 Great for debugging. 非常适合调试。
    • you can even change the styling directly, by editing or adding rules. 您甚至可以通过编辑或添加规则直接更改样式。 You would have to save these to you css file afterwards of course, but it is great for developing and testing. 当然,您以后必须将它们保存到css文件中,但这对于开发和测试非常有用。

You should really learn to work with the inspector, it is a very powerful tool that can make developing so much easier! 您应该真正学会与检查员一起工作,这是一个非常强大的工具,可以使开发变得如此容易!

var col = document.getElementById(your_div_id).style.backgroundColor;

You can basically do this with every attribute you can think of. 您基本上可以使用您能想到的每个属性来执行此操作。 If it's not a style attribute, just remove .style . 如果不是style属性,则只需删除.style

If there is a dash in the attribute name, the letter after the dash is often just capitalized, and the dash is removed. 如果属性名称中有破折号,则破折号后的字母通常只是大写,并且删除了破折号。

you would have to do a search, get the element then all its parents in the hierarchy and check their values. 您将需要进行搜索,获取元素,然后获取层次结构中的所有父元素并检查其值。

If you are just looking at a page and wanting to know and not really needing to do this programmatically you can use a browsers F12 console to see where it gets its properties from 如果您只是在浏览页面而又不想真正地以编程方式进行操作,则可以使用浏览器F12控制台查看从何处获取其属性。

For instance chromes console window when you select a html tag will show the css attributes and where they come from on the right side. 例如,chromes控制台窗口在您选择html标签时将显示css属性以及它们来自右侧的位置。

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

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