为包含 HTML 标记的字符串中的元素设置 Style 属性

Set Style property for an element from string containing HTML Tags

本文关键字:元素 设置 Style 属性 字符串 HTML 包含      更新时间:2023-09-26

我有一个HTML字符串,包含buttonlispan等元素。

我想根据类名将一些样式应用于按钮。

例如:如果

button.btn {
   some styles
} 

button.btn-success {
    some other styles
}

添加样式后,我必须将此 html 字符串添加到正文中。我如何在 Jquery 中做到这一点。请帮助我。

提前谢谢。

尝试如下:

$('button').each(function(){
    if($(this).hasClass("btn")){
    // add style here
    }
    if($(this).hasClass("btn-success")){
    // add style here
    }
});