如何在HTML标记上添加类

时间:2015-08-31 20:57:27

标签: javascript jquery html css

如果代码存在,我正在尝试在 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类会自动应用它。

3 个答案:

答案 0 :(得分:5)

尝试将<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>

SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Text file (*.txt)|*.txt";
if (sfd.ShowDialog() == DialogResult.OK)
{
    File.WriteAllText(sfd.FileName, container.Text);
    MessageBox.Show("File " + sfd.FileName + " created at " + DateTime.Now.ToString());
    container.ResetText();
}

答案 1 :(得分:0)

如果要将样式应用于HTML标记,请使用标记名these docs

创建样式

对于你的情况

$Home\Documents\Profile.ps1

这适用于文档上的所有文本区域标记。 这是我从你的源代码创建的JSFiddle

Link here

答案 2 :(得分:-2)

您不需要$(window).load(function () {});,只需离开此行:

$('textarea').addClass('black');

Here是演示。

&#13;
&#13;
$('textarea').addClass('black');
&#13;
.black {
   background:#000;
   color:white;
}
&#13;
<div>
    <textarea>content</textarea>
</div>
&#13;
&#13;
&#13;