Finding Javascript-Applied Inline Styles to Debug Javascript

China☆狼群 提交于 2019-12-30 08:03:43

问题


SCENARIO: I am taking over management of a website where the former web developer has unfortunately spread out many relevant functions over many long JS files. It is hard for me to find where an inline CSS style is coming from in the JS or what function is applying this style directly to the element.

QUESTION: Is there a method of reverse engineering an element's inline styles to see where they are coming from?


回答1:


A possible way is to add a DOM breakpoint in Chrome Dev Tools

For that to work, you have to add the breakpoint before the style is added. That can be tricky, but you can force a breakpoint before loading any JavaScript by adding the following immediately after the HTML element in question

<script>debugger</script>

Try it on the following code

  • Click on the "Run Code Snippet" button below
  • Open Dev Tools
  • Right click the paragraph
  • Select Break On... -> Attribute modifications
  • Click the "Add border color" button
  • Voila, your debugger stops at the code that is modifying the style

window.onload = function() {
    document.querySelector('button').addEventListener('click', function() {
         document.querySelector('p').style.border = '2px solid red';    
    });
}
<p> Sample Paragraph </p>
<button>Add border color</button>


来源:https://stackoverflow.com/questions/29657953/finding-javascript-applied-inline-styles-to-debug-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!