jquery - 检查页面上是否有特定的字形图标,如果有,请更改为其他字形

标签 jquery css glyphicons

我正在尝试创建一个函数来检查页面上是否有特定的字形图标,如果是,我希望它显示其他内容。

这是我的代码,但运气不佳

var span;
$('span').each(function(){
  if($(this).html() == 'glyphicon glyphicon-ok'){
    $(this).parent('li').html('✔').css({"color" : "blue"});
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<span class="glyphicon glyphicon-ok"></span>

最佳答案

Jquery .attr() 方法来挽救局面

通过使用.attr()方法我们可以获得元素的任何属性。

因此,通过使用: $(this).attr('class') 我们可以查看它是否与您想要替换的字形匹配,并按照您的描述更改其值。

贝娄是一个工作示例。

var span;
$('span').each(function(){
  if($(this).attr('class') == 'glyphicon glyphicon-ok'){
    $(this).html('✔').css({"color" : "blue"});
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<span class="glyphicon glyphicon-ok"></span>

关于jquery - 检查页面上是否有特定的字形图标,如果有,请更改为其他字形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40491144/

相关文章:

javascript - 导航栏在滚动条上出现和消失

单击图标的 jQuery 日期选择器

css - 在 next.js 中,如何将自定义 css 类和 Bootstrap 类同时应用于元素

twitter-bootstrap - 我可以从 CDN 获取 Bootstrap Glyphicons 吗?

css - 压缩 css 文件导致 glyphicon 字体无法呈现 : Resources interpreted as Font but transferred with MIME type text/plain

jquery - .toggleClass 在类之间切换但具有多个 ID

javascript - 如何处理与 jQuery 不冲突的对象类

css - 为什么在 CSS3 中启用硬件加速会降低性能?

html - 溢出属性在 JSfiddle 中有效,但在 chrome/safari 中无效

firefox - Bootstrap glyphicons Firefox 问题