简体   繁体   English

当它说element.style时如何找到代码的来源?

[英]How to find the source of the code when it says element.style?

I have no idea how to find the source of this element style codes. 我不知道如何找到这个元素样式代码的来源。 Such as at the right part of the Chrome Element Inspector Tool it shows this CSS code: 例如,在Chrome Element Inspector Tool的右侧,它显示了这个CSS代码:

element.style {
    position: absolute;
    left: 0px;
    top: 0px;
    width: 486px;
    height: 200px;
    overflow: hidden;
    -webkit-user-select: none;
    background-color: #FFF;
    border: 1px solid #ABABAB;
}

and at the left part there is this : 在左边部分有这样的:

<div draggable="false" style="position: absolute; left: 0px; top: 0px; width: 486px; height: 200px; overflow: hidden; -webkit-user-select: none; background-color: white; border: 1px solid rgb(171, 171, 171);"></div>

I've looked into the modules files but I only see JavaScript and HTML files without the above line? 我查看了模块文件,但我只看到没有上述行的JavaScript和HTML文件?

element.style just tells you that the styles are added to the element through the style attribute or JavaScript, but not via an external CSS file. element.style只是告诉您样式是通过style属性或JavaScript添加到元素中的,而不是通过外部CSS文件。 You might check you JS files for these properies if you can't find the them in your markup. 如果您在标记中找不到它们,可以检查JS文件中的这些属性。

That's because those styles are inline . 那是因为这些样式是内联的 Meaning they use the style attribute of an HTML tag to define its CSS properties. 这意味着他们使用HTML标记的style属性来定义其CSS属性。 That CSS is not in a file or elsewhere. CSS不在文件或其他地方。 It is being defined "within" the element it is being applied to. 它被定义在它所应用的元素“内”。

That specific line of HTML doesn't need to exist in any file for that div to exist in the DOM. 该特定的HTML行不需要存在于该div中存在于DOM中的任何文件中。 For example, this Javascript would create a div just like that: 例如,这个Javascript会像这样创建一个div:

var div = document.createElement('div');
div.setAttribute('draggable', 'false');
div.style.position = 'absolute';
div.style.overflow = 'hidden';
div.style.cssText += 'left: 0px; top: 0px; width: 486px; height: 200px; -webkit-user-select: none; background-color: white; border: 1px solid rgb(171, 171, 171);';
document.body.appendChild(div);

Try searching your JS files for -webkit-user-select . 尝试在JS文件中搜索-webkit-user-select That style property is used rarely enough that it should help you find the relevant section of Javascript code. 该样式属性很少使用,它可以帮助您找到Javascript代码的相关部分。

I just ran into a similar problem, and it turned out that AdBlocker (browser plug-in) was inserting in-line styles to hide images on my site that it thought were ads. 我刚遇到类似的问题,结果发现AdBlocker(浏览器插件)正在插入内嵌样式来隐藏我认为是广告的网站上的图片。 Try disabling some or all of your browser plug-ins and see if they are interfering with the way your page is rendered. 尝试禁用部分或全部浏览器插件,看看它们是否干扰了网页的呈现方式。

This might not be exactly what you are looking for, but if you click on the small '+' icon on the top right of the inspector, it lets you add your styles to a css class that is already attached to your element. 这可能不是您正在寻找的,但如果您单击检查器右上角的小“+”图标,则可以将样式添加到已附加到元素的css类中。 When yo do so, you will be able to click on the inspector-stylesheet link that appears on the right of the defined style. 当您这样做时,您将能够单击显示在已定义样式右侧的检查器样式表链接。 I think thats the closest you can get to defining some sort of temporary stylesheet that will contain all of your custom styles. 我认为最接近你可以定义某种包含所有自定义样式的临时样式表。

Thats what I do for quick testing. 这就是我做的快速测试。 Set some classes in the html, then add more custom styles in the inspector side panel - because its easy to edit and/pick colors in there. 在html中设置一些类,然后在检查器侧面板中添加更多自定义样式 - 因为它易于编辑和/或选择颜色。 Then open inspector-stylesheet and copy them all once I am done. 然后打开检查器样式表,并在完成后将它们全部复制。

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

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