当ajax请求为done
时,会使用jquery生成此html,并将其附加到表单中:
<div class="notice-success clickable close" id="notice"><p class="icon">response message</p></div>
类notice-success
也可以是:
notice-error
,
notice-warning
,
notice-info
视情况或反应而定。
这是我的javascript的简化部分:
let $form = $('form#'+$(this).attr('id')); // use this specific form
let $formNotice = $form.find('#notice'); // locate #notice element (if any)
if($formNotice.length){ // if there's a #notice, then update it to reflect the new response.
$formNotice.removeClass('[class^=notice-]'); // <-- this doesn't work
$formNotice.addClass('notice-'+data['type'])
$formNotice.html('<p class="icon">'+data['msg']+'</p>');
} else {
$('<div class="notice-'+data['type']+' clickable close" id="notice"><p class="icon">'+data['msg']+'</p></div>').hide().prependTo($form).slideDown("slow");
}
.removeClass('[class^=notice-]')
不起作用。
删除其他类之一(clickable
或close
)就可以了。
$formNotice.addClass
和$formNotice.html
也可以。
那么我的问题就是如何更改/删除任何notice-
类?
我还尝试了$formNotice.removeClass('class');
来删除所有类,然后使用.addClass()
将它们全部放回原处。但这也不起作用。.一遍又一遍地重新提交表单时,我只是添加了很多类。
有什么建议吗?
编辑(已解决):
通过使用此代码,我可以查找和删除以宽度notice-
开始的类(在我的情况下)。
-https://stackoverflow.com/a/28608620/1355562
感谢您指出我正确的方向...
然后,在使用notice-?
添加新的.addClass
类时,它在末尾添加-某种程度上破坏了代码。 background-color
以外的所有css属性均未正确呈现。
经过一些测试,结果证明该类必须是第一个。
通过了解这一点,我不得不找到一种在班级列表的开头添加该班级的方法。所以我发现了这个
-https://stackoverflow.com/a/23839395/1355562
答案 0 :(得分:0)
你能尝试
$formNotice.removeClass('notice-error notice-warning notice-info');