我想在 jQuery 中做 addClass,
当我尝试下面的代码时,结果就像图片 1
$('#contineButton').html('Continue').addClass('fa fa-chevron-right');
$('#contineButton').html('Continue').after().addClass('fa fa-chevron-right');
但是,我想得到如下图 2 的结果
原始按钮代码:
<button type="button" class="btn btn-success next-step" id="contineButton">
${vo.continueButtonText} <i class="fa fa-chevron-right"></i>
</button>
最佳答案
你正在使用 after() 而你应该使用 append()
$('#contineButton').html('Continue').append('<i class="fa fa-chevron-right"></i>');
解释:
您在第一次尝试中使用了 html->addClass 会将类添加到当前对象(按钮),在第二次尝试中(使用 after)将尝试在之后插入的空元素中执行此操作。
html
完全替换了按钮的内容,所以 <i>
不存在了。
关于jquery - 在 jQuery 中的所需元素之后添加类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41763422/