【问题标题】:Adding and removing class with onClick for dynamic button使用 onClick 为动态按钮添加和删除类
【发布时间】:2013-09-15 17:06:43
【问题描述】:

我有这些按钮

<button id="btn1" type="button" class="btn btn-default btn-sm margin-left-1">
    <span class="glyphicon glyphicon-chevron-down"></span>
</button>

<button id="btn2" type="button" class="btn btn-default btn-sm margin-left-1">
    <span class="glyphicon glyphicon-chevron-down"></span>
</button>

...etc

我想删除类 glyphicon-chevron-down 并在单击该按钮后添加 glyphicon-chevron-up .. 它的 twitter 引导程序正在崩溃,但我想要一个带有图标的按钮 :-)
记录的id可以放在button标签里,也可以放在span标签里,没关系,有什么办法可以检查应用的CSS吗?
例如,${#element}.existCSS?('glyphicon-chevron-down')

【问题讨论】:

  • $("#element").hasClass('glyphicon-chevron-down')
  • 感谢 DevIshOne 就是这样。
  • @devo:在编辑时,请不要只添加标签——尽量修复。

标签: jquery css twitter-bootstrap twitter-bootstrap-3


【解决方案1】:

试试:

if( $(<#element>).hasClass("glyphicon-chevron-down") ) {
 //Your code
}

【讨论】:

  • 那只是伪代码中元素选择器的占位符。
【解决方案2】:

你可以试试这个:

$('.btn-default').click(function() { $(this).find('.glyphicon-chevron-down').removeClass('glyphicon-chevron-down').addClass('glyphicon-chevron-up');
});

看看这个 - http://jsfiddle.net/9NDyL/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-03
    • 2013-10-26
    • 1970-01-01
    相关资源
    最近更新 更多