时间:2023.10.23
时长:80 min
base:帝都
面试岗位:前端 秋招
let computedStyle = window.getComputedStyle(element, ":before");
let content = computedStyle.getPropertyValue("content");
console.log(content);
我写的代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
body {
margin: 0;
font-size: 14px;
}
</style>
<title></title>
</head>
<body>
<div id="jsContainer">
<ul class="js-test" id="jsParent">
<li data-index="0">
<div id="1111">222222222222222222222222</div>
</li>
<li data-index="1">2</li>
</ul>
<span style="font-weight: bold;">3</span>
4
</div>
<script type="text/javascript">
function dom2json() {
const root = document.getElementById('jsContainer')
const result = dfs(root)
console.log(result)
function dfs(root) {
const result = {}
result.tag = root.tagName
const names = root.getAttributeNames() // arr
const attrs = {}
for (const name of names) {
attrs[name] = root.getAttribute(name)
}
result.attributes = {
...attrs
}
const arr = root.children
if (arr.length === 0) {
result.content = root.innerText
}
const children = []
for (const item of arr) {
const temp = dfs(item)
children.push(temp)
}
if (children.length) {
result.children = children
}
return result
}
}
dom2json()
</script>
</body>
</html>
难度不高,但是属性值记不住,API也记不住。。最后共享屏幕然后用webstorm靠代码提示写出来的,有些API不知道,直接共享屏幕百度查API的
#前端##面经##秋招##360#