在jQuery .attr()中分配时,不会应用CSS类

时间:2012-11-02 07:14:19

标签: jquery css attributes

我正在使用jQuery操作链接的data-complete-text属性。我的代码看起来像这样。

var complete =  _self.attr("data-complete-text");

_self.attr("data-complete-text","<span class='symbol-done-tick'></span><span class='acheived-text'>"+complete+"</span>");

那就是我将html spans分配给这个属性以获得所需的效果,它工作正常。我的意思是,跨度插入正确,但我在这些跨度中指定的CSS类没有任何影响。我不知道为什么。帮助我。

1 个答案:

答案 0 :(得分:1)

如果您尝试设置data-complete-text以便Bootstrap的Button将使用它,则在设置data-complete-text属性后,您还必须设置按钮的状态:< / p>

_self.button('complete');

文档(例如它)不它支持HTML,但它似乎在我的测试中起作用:Live Example | Source

HTML:

<button id="target" type="button" class="btn btn-primary" data-loading-text="Loading...">Original Text</button>

JavaScript的:

jQuery(function($) {

  // Get the button
  var _self = $("#target");

  // Set the attribute
  _self.attr("data-complete-text",
             "<span class='foo'>Complete</span>");

  // Wait for click
  _self.one("click", function() {

    _self.button('complete');
  });

});

在上文中,当您将按钮状态设置为foo时,spandata-complete-text上的'complete'课程正常工作。

相关问题