gpt4 book ai didi

javascript - 如何在 HTML 标签上添加类

转载 作者:太空宇宙 更新时间:2023-11-03 23:11:33 25 4
gpt4 key购买 nike

我正在尝试在 HTML 标签上应用一个 class,如果该标签存在的话。

我试过这段代码:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>index</title>

<style>
.black
{
background:#000;
color:white;
}

</style>
<script type="text/javascript">
$(window).load(function () {
$('textarea').addClass('black');
});
</script>
</head>
<body>

<div>


<textarea>content</textarea>


</div>

<script src="jquery.js"></script>

</body>
</html>

我想要什么:如果 HTML 正文包含一个 textarea 标签,那么 .black 类将自动应用它。

最佳答案

尝试移动 <script src="jquery.js"></script>之前 <script></script>包含调用 $(window).load()对于 jQuery()调用时定义

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>index</title>
<style>
.black {
background: #000;
color: white;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<script type="text/javascript">
$(window).load(function() {
$('textarea').addClass('black');
});
</script>
</head>

<body>
<div>
<textarea>content</textarea>
</div>
</body>

</html>

关于javascript - 如何在 HTML 标签上添加类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32319857/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com