Nik*_*ger 7 html javascript css setattribute
我是javascript的新手,想要编写一个html网站的标题.JS:当窗口宽度小于1215px时 - >左边部分变为100%宽度; 标题的右侧部分为0
我总是得到"无法读取属性'setAttribute'的null"错误!
请帮忙!码:
if (window.innerWidth < 1215) {
document.getElementById("#headerLeft").setAttribute("style", "width:100%");
document.getElementById("#headerRight").setAttribute("style", "width:0%");
} else {
document.getElementById("#headerLeft").setAttribute("style", "width:50%");
document.getElementById("#headerRight").setAttribute("style", "width:50%");
}Run Code Online (Sandbox Code Playgroud)
body {
margin:0;
}
header{
height:100px;
width:100%;
}
#headerLeft {
background-color:#FF7043;
height:100px;
float:left;
}
#headerRight {
background-color:#FFFFFF;
height:100px;
float:right;
}
.headerTitle {
font-family:'Roboto', sans-serif;
margin:15px;
font-size:70px;
}
#headerRight .headerTitle {
text-align:left;
color:#FF7043;
font-weight:300;
}
#headerLeft .headerTitle {
text-align:center;
color:#FFFFFF;
font-weight:900;
}Run Code Online (Sandbox Code Playgroud)
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>example</title>
<!-- css -->
<link type="text/css" rel="stylesheet" href="style.css">
<!-- fonts -->
<link href='https://fonts.googleapis.com/css?family=Roboto:400,500,700,900,100,300' rel='stylesheet' type='text/css'>
<!-- javascripts -->
<script language="javascript" type="text/javascript" src="script.js"></script>
</head>
<body>
<header>
<div id="headerLeft">
<p class="headerTitle">example</p>
</div>
<div id="headerRight">
<p class="headerTitle">example</p>
</div>
</header>
<nav>
</nav>
<main>
</main>
<footer>
</footer>
</body>
</html>Run Code Online (Sandbox Code Playgroud)
myn*_*fey 10
这是因为所有调用document.getElementById都没有找到任何内容,因为没有任何带有这些ID的元素.看起来像jQuery习惯,#从你的ID中删除.
| 归档时间: |
|
| 查看次数: |
52003 次 |
| 最近记录: |