改变 HTML 样式 Object.style.property=new style;

本文介绍如何使用JavaScript通过HTML DOM更改HTML元素的样式。通过示例代码展示了如何修改元素的颜色、背景颜色及宽度等样式。

改变 HTML 样式

HTML DOM 允许 JavaScript 改变 HTML 元素的样式。如何改变 HTML 元素的样式呢?

语法:

Object.style.property=new style;

注意:Object是获取的元素对象,如通过document.getElementById("id")获取的元素。

基本属性表(property):

注意:该表只是一小部分CSS样式属性,其它样式也可以通过该方法设置和修改。

看看下面的代码:

改变 <p> 元素的样式,将颜色改为红色,字号改为20,背景颜色改为蓝:

<p id="pcon">Hello World!</p>
<script>
   var mychar = document.getElementById("pcon");
   mychar.style.color="red";
   mychar.style.fontSize="20";
   mychar.style.backgroundColor ="blue";
</script>

结果:

任务

现在我们来改变下HTML中元素的CSS样式:

1. 在右边编辑器中,第12行补充代码,修改h2标签的样式,将颜色设为红色。

2. 在右边编辑器中,第13行补充代码,修改h2标签的样式,将背景颜色设为灰色(#CCC)。

3. 在右边编辑器中,第14行补充代码,修改h2标签的样式,将宽设为300px。


<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>style样式</title>
</head>
<body>
  <h2 id="con">I love JavaScript</h2>
  <p> JavaScript使网页显示动态效果并实现与用户交互功能。</p>
  <script type="text/javascript">
    var mychar= document.getElementById("con");
    mychar.style.color="yellow";
    mychar.style.backgroundColor:"#CCC";
    mychar.style.width=300px;
  </script>
</body>
</html>



<CheckBox IsChecked="{Binding IsAutoPassEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center" Command="{Binding EnableOrDisableAutoPassCommand}"/> <TextBlock Style="{StaticResource CollaborationBulletinTailLabelStyle}" Text="{globalizer:GlobalizedResource CollaborationBulletinView.Labels.EnableAutoPublish, FallbackValue='Enable Auto Pass'}"/> 我在做一个wpf项目,我想将这个check box添加一个底色当我打钩后是绿色 当我未打钩是红色 就像下面这个button代码 <Button Height="22" Command="{Binding EnableOrDisableAutoPassCommand}"> <Button.Style> <Style TargetType="{x:Type Button}" BasedOn="{StaticResource CollaborationBulletinButtonStyle}"> <Setter Property="Content" Value="{globalizer:GlobalizedResource CollaborationBulletinView.Labels.EnableAutoPublish, FallbackValue='Enable Auto Pass'}" /> <Style.Triggers> <DataTrigger IsChecked="{Binding IsAutoPassEnabled, UpdateSourceTrigger=PropertyChanged}" Value="False"> <Setter Property="Content" Value="{globalizer:GlobalizedResource CollaborationBulletinView.Labels.EnableAutoPublish, FallbackValue='Enable Auto Pass'}" /> <Setter Property="Background" Value="Green" /> </DataTrigger> <DataTrigger IsChecked="{Binding IsCoverageEnabled, UpdateSourceTrigger=PropertyChanged}" Value="True"> <Setter Property="Content" Value="{globalizer:GlobalizedResource CollaborationBulletinView.Labels.CloseAutoPublish, FallbackValue='Close Auto Pass'}" /> <Setter Property="Background" Value="Red" /> </DataTrigger> </Style.Triggers> </Style> </Button.Style> </Button>
最新发布
08-05
public bool? IsChecked { get { object value = GetValue(IsCheckedProperty); if (value == null) { return null; } return (bool)value; } set { SetValue(IsCheckedProperty, value.HasValue ? BooleanBoxes.Box(value.Value) : null); } } <CheckBox IsChecked="{Binding IsAutoPassEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center" Command="{Binding EnableOrDisableAutoPassCommand}"/> <TextBlock Style="{StaticResource CollaborationBulletinTailLabelStyle}" Text="{globalizer:GlobalizedResource CollaborationBulletinView.Labels.EnableAutoPublish, FallbackValue='Enable Auto Pass'}"/> 在这个wpf中 我想将他ischeck这个逻辑改到从check box改成 button <Button Height="22" Command="{Binding EnableOrDisableAutoPassCommand}"> <Button.Style> <Style TargetType="{x:Type Button}" BasedOn="{StaticResource CollaborationBulletinButtonStyle}"> <Setter Property="Content" Value="{globalizer:GlobalizedResource CollaborationBulletinView.Labels.EnableAutoPublish, FallbackValue='Enable Auto Pass'}" /> <Style.Triggers> <DataTrigger Binding="{Binding IsAutoPassEnabled, UpdateSourceTrigger=PropertyChanged}" Value="False"> <Setter Property="Content" Value="{globalizer:GlobalizedResource CollaborationBulletinView.Labels.EnableAutoPublish, FallbackValue='Enable Auto Pass'}" /> <Setter Property="Background" Value="Green" /> </DataTrigger> <DataTrigger Binding="{Binding IsCoverageEnabled, UpdateSourceTrigger=PropertyChanged}" Value="True"> <Setter Property="Content" Value="{globalizer:GlobalizedResource CollaborationBulletinView.Labels.CloseAutoPublish, FallbackValue='Close Auto Pass'}" /> <Setter Property="Background" Value="Red" /> </DataTrigger> </Style.Triggers> </Style> </Button.Style> </Button>
08-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值