马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我给一个页面添加了随即改变背景色的js,但是在这之后原来的css失效了,这是怎么回事?//这是更改评论盒子背景色的脚本
document.addEventListener('DOMContentLoaded', function() {
let box = document.querySelectorAll(".comment");
for (let i = 0; i < box.length; i++) {
setRandomBackgroundColor(box[i]);
}
});
//随机背景色函数
function setRandomBackgroundColor(element) {
let letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
element.style.backgroundColor = color;
}
<%@ page import="java.util.List" %>
<%@ page import="edu.zjku.bean.Comment" %>
<%@ page import="edu.zjku.bean.Blog" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>博客详情</title>
<link rel="stylesheet" href="style/public.css"/>
<script src="script/randomColor.js"/>
<style>
.comment {
display: flex;
align-items: center;
margin: 20px auto;
padding-left: 20px;
width: 100%;
height: 50px;
color: #fff;
line-height: 50px;
text-align: center;
/*background: teal;*/
clip-path: polygon(0 0, 88% 0, 88% 35%, 95% 50%, 88% 65%, 88% 100%, 0 100%);
border: 1px solid teal;
}
</style>
</head>
<body>
<jsp:include page="header.jsp"/>
<main>
<%-- 从会话域获取博客及其评论--%>
<%
Blog blog = (Blog) session.getAttribute("blogDetail");
List<Comment> commentList = (List<Comment>) session.getAttribute("commentList");%>
<div class="blog">
<h2><%=blog.getTitle()%>
</h2>
<p><%=blog.getText()%>
</p>
<p><%=blog.getUser()%>
</p>
<p><%=blog.getTime()%>
</p>
</div>
<%
for (Comment comment : commentList) {
%>
<div class="comment">
<p><%=comment.getContent()%>
</p>
<p><%=comment.getUsername()%>
</p>
<p><%=comment.getTime()%>
</p>
</div>
<%}%>
</main>
<jsp:include page="aside.jsp"/>
<jsp:include page="footer.jsp"/>
</body>
</html>
|