JS问题求教大神为什么我的代码里自动生成的p没有获到到CSS样式,而且continue; 好像也没有起到作用
下面是我写的JS是先按条件生成一系列p并在里面附加了CSS样式;再让循环的条件等于3时就跳过3这个p直接从4开始生成;
但生成了p后,p里面的Class没有作用;后面的判断条件也没起到做用。求大神们帮助小弟解决一下哈。谢谢大神们。请帮助指出我写的代码错误原因与如何解决哈。最好是有解决的案例谢谢哈。
如题我写的代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style>
.calss_p {
width: 400px;
height: 200px;
background-color: #999999;
margin-bottom: 10px;
display: block;
}
</style>
<script>
window.onload = function Op() {
for (var i = 0; i <= 10; i++) {
document.write('<p id="' + i + '" class="calss_p">这个是自动创建的编号是' + i + '</p>');
if (i == 3) {
alert("这是内容");
content;
} else {
alert("这是假的");
}
}
}
</script>
</head>
<body>
<p class="calss_p">这个不是用JS生成的</p>
</body>
</html>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
应该是这样子的:
没有样式的原因:document.write把style的代码覆盖了
continue失败的原因: 执行代码应该写在continue后面,continue才能阻止以后的代码执行