简体   繁体   English

动态更改CSS类名称

[英]Change CSS class name dynamically

I need to change the CSS class name of all the elements in a page with a particular class name (.k-textbox). 我需要使用特定的类名(.k-textbox)更改页面中所有元素的CSS类名。 I tried the below code but it does not hit inside the .each() function 我尝试了以下代码,但未在.each()函数内部命中

<script>
    $(document).ready(function () {    
        $(".k-textbox").each(function () {
            //alert("a");
            $(this).removeClass("k-textbox");
            $(this).addClass("input-medium");
        });
    });
    </script> 

In the page i have a 3rd party grid control. 在页面中,我有一个第三方网格控件。 the CSS class i have mentioned is inside that third party grid control. 我提到的CSS类位于该第三方网格控件内。

below is the DOM object: 下面是DOM对象:

在此处输入图片说明

You should try to use chaining API provided by jQuery library: 您应该尝试使用jQuery库提供的链接API:

$(".k-textbox").removeClass("k-textbox").addClass("input-medium");

edit: 编辑:

As long as the elements are created dynamically you could try to run this code after those elements are created. 只要动态创建元素,就可以在创建这些元素之后尝试运行此代码。 But if you don't know when they are inserted into the code and doesn't have control over them you could try write simple watch function, ie: 但是,如果您不知道何时将它们插入代码中并且无法控制它们,则可以尝试编写简单的watch函数,即:

var watchTimer = setInterval(function () {
      var inputs = $('.k-textbox');
      if (inputs.length) {
          // clear interval
          clearInterval(watchTimer);

          // change class
          inputs.removeClass("k-textbox").addClass("input-medium");
      }
    }, 100);

使用addClass()removeClass()

$(".k-textbox").removeClass("k-textbox").addClass("input-medium");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM