<%@ Page Language="C#" AutoEventWireup="true" CodeFile="动态改变字体颜色大小.aspx.cs" Inherits="动态改变字体颜色大小" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<style type="text/css">.style1 { font: bold 16px ; color: #0000FF}
.style2 { font-size:12px; color:#00ff00}
</style>
<script>
function change(){
document.getElementById("p1").style="font: bold 16px ; color: #0000FF";//该行不可以改变
//document.getElementById("p1").className="style1"//该行可以改变
//是不是动态改变元素属性必须得把属性值写到css里才可以?
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<p class="style2" id="p1" onmouseover="change()" >
鼠标在这段文字上,改变文字的大小、颜色、加粗,鼠标离开时恢复原样。
</p>
</div>
</form>
</body>
</html>
请看注释,回答注释的问题?
<head runat="server">
<title>无标题页</title>
<style type="text/css">.style1 { font: bold 16px ; color: #0000FF}
.style2 { font-size:12px; color:#00ff00}
</style>
<script>
function change(){
document.getElementById("p1").style="font: bold 16px ; color: #0000FF";//该行不可以改变
//document.getElementById("p1").className="style1"//该行可以改变
//是不是动态改变元素属性必须得把属性值写到css里才可以?
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<p class="style2" id="p1" onmouseover="change()" >
鼠标在这段文字上,改变文字的大小、颜色、加粗,鼠标离开时恢复原样。
</p>
</div>
</form>
</body>
</html>
请看注释,回答注释的问题?
解决方案 »
- 请教一个JS跨域的小白问题
- 如何使用CSS+DIV做出一个类似于Google地图那样的界面
- 如何能让这个带背景的层也在最下面,是不是高手无所谓,重在参与,给点建议.
- 怎么能够让浏览器操纵不了, 直到我要做的事情做完?
- javascript的语句 FOR...IN 不能遍历对象属性了。大家帮帮忙!!!
- 怎么实现让一个<div id=r></div>中的代码隔一段时间向上滚动?
- 读取文件内容(JS市斤进,在线等)
- 如何使一张图片点击一下变成另一张,再点击一下变回这一张???
- 请wsj(骆驼)进来
- 如何做一个只可以在本窗口,不可以转到其它窗口的窗口
- 关闭页面前,js判断是刷新,关闭,还是转向其他页面
- 子画面刷新父画面
style是只读的
document.getElementById("p1").style="font: bold 16px ; color: #0000FF";//该行不可以改变
==>
document.getElementById("p1").style["font"] ="bold 16px";//该行不可以改变
document.getElementById("p1").style["color"] = "#0000FF";
楼上的正解不怕麻烦还可以用以下方式
document.getElementById("p1").style.font="16px";
document.getElementById("p1").style.color="#0000FF";
$("#id").css("css_name","css_value");jquery封装过,比较好用,代码量少
document.getElementById("p1").style="font: bold 16px ; color: #0000FF";//该行不可以改变
//document.getElementById("p1").className="style1"//该行可以改变
//是不是动态改变元素属性必须得把属性值写到css里才可以?
}改为:
function change(){
document.getElementById("p1").style.cssText="font: bold 16px ; color: #0000FF";//cssText对应style属性内部的内容
document.getElementById("p1").className="style1"//className对应于class属性的值
}