jquery - 在 jQuery 中的所需元素之后添加类

标签 jquery css

我想在 jQuery 中做 addClass,

当我尝试下面的代码时,结果就像图片 1

 $('#contineButton').html('Continue').addClass('fa fa-chevron-right');
 $('#contineButton').html('Continue').after().addClass('fa fa-chevron-right');

enter image description here

但是,我想得到如下图 2 的结果

enter image description here

原始按钮代码:

<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/

相关文章:

jquery - Facebook API - 需要 "User Is Now Logged In"事件吗?

javascript - 在 Javascript 中动画化 div 外观

javascript - Highstock 月份数据与 Jalali/波斯日期分组

ruby-on-rails - 使用 Ruby on Rails 3 表单助手的多个 CSS 类/id 名称

jquery - 使用 jQuery 获取所有 mailto 链接

jquery - 在行悬停时展开 TH 元素

html - 使图像与表格行的其余部分高度相同

css - Edge 不支持自定义光标?

html - 输入字段无法在 w3.css 中显示文本

css - 如何在 HTML 中显示反向排序列表?