javascript - 在单击按钮时更改文本颜色,并(如果可能)在单击时从更改颜色中排除某些文本或元素
问题描述
如何制作一个按钮,何时onclick
更改页面上的所有文本更改颜色,包括 div 内或 div 外的文本。如果可能的话,我可以从更改颜色中排除某些文本,例如页脚或文本链接吗?
HTML
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css">
<link rel="stylesheet" href="css/all.css"></link>
</head>
<body>
<div class="changecolor">
<ul>
<button type="button" class="redbutton" style="background-color:red"></button>
<button type="button" class="bluebutton" style="background-color:blue"></button>
<button type="button" class="greenbutton" style="background-color:green"></button>
<button type="button" class="blackbutton" style="background-color:black"></button>
</ul>
</div>
<div class="registerbox">
<div class="inner-registerbox">
<div class="registerbox-icon"><i class="fas fa-user-plus"></i></div>
<div class="registerbox-front">
<h2>BORANG PENDAFTARAN MURID</h2>
<form method='POST' action=''>
<form>
<div class="box-firstline">
<label for="idmurid">ID Murid</label> <br />
<input type="text" name="idmurid" class="input-box2" required>
<label for="namamurid">Nama Murid</label> <br />
<input type="text" name="namamurid" class="input-box3" required>
</div>
<div class="box-secondline">
<label for="namakelas">Nama Kelas</label> <br />
<input type="text" name="namakelas" class="input-box2" required>
<label for="katalaluan_murid">Kata Laluan</label> <br />
<input type="password" name="katalaluan_murid" class="input-box3" required minlength='5' required maxlength='20' required>
<label>Kata Laluan Sekali Lagi</label> <br />
<input type="password" name="katalaluan_murid2" class="input-box3" required minlength='5' required maxlength='20' required>
</div>
<input type='submit' value='Daftar' class="registerbutton">
<p>Sudah mempunyai akaun? <a href="index.php">Log Masuk</a></p>
</div>
</div>
</div>
<?PHP include('footer.php');?>
</body>
</html>
解决方案
为按钮添加单击处理程序changeColor
,并为要更改某些类的元素提供。没有allow-change
该类的其他所有内容都将被忽略。
function changeColor(color){
$('.allow-change').css('color', color);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<div class="changecolor">
<ul>
<button type="button" class="redbutton" style="background-color:red; width: 20px; height: 10px;" onclick="changeColor('red')"></button>
<button type="button" class="bluebutton" style="background-color:blue; width: 20px; height: 10px;" onclick="changeColor('blue')"></button>
<button type="button" class="greenbutton" style="background-color:green; width: 20px; height: 10px;" onclick="changeColor('green')"></button>
<button type="button" class="blackbutton" style="background-color:black; width: 20px; height: 10px;" onclick="changeColor('black')"></button>
</ul>
</div>
<p class="allow-change">This text color can be changed</p>
<p>This text color cannot be changed</p>
</body>
推荐阅读
- r - 如何根据两个变量更改 geom_point 大小?
- javascript - 使用某个类单击行后隐藏所有行
- c++ - 如何在 C++ 中重新定义一对
- python - Matplotlib:如何在 Arc 中绘制边缘线 - 补丁
- react-native - Rendering React Native 'Tile' in a single row
- android - 从初始屏幕中删除标题栏
- xamarin.forms - Xamarin Switch Toggled 事件:获取列表的绑定项
- lua - Lua - 在不使用带有 2 个单独脚本的调试库的情况下获取本地化变量或函数
- rest - 使用 HMAC 时随机数 RESTful 是如何实现的?
- r - R purr:将精度映射到预测对象并返回精度