添加/删除类在 IE 中不起作用

发布于 2024-12-09 15:15:46 字数 2032 浏览 1 评论 0原文

.toggle(
  function() { 
      hiddenElements.show();
      $(this).text('Collapse');
      $(".accToggler").removeClass().addClass("accToggler2");
  }, 
  function() { 
      hiddenElements.hide();
      $(this).text(showCaption);
      $(".accToggler2").removeClass().addClass("accToggler");
  }
)

单击原始 .accToggler 按钮后,切换功能似乎不起作用。类不会被添加/删除到按钮。

我把它改成这样:

                      .toggle(
                          function() { 
                              hiddenElements.show();
                              $(this).text('Collapse');
                              $("#accTogg").removeClass("accToggler").addClass("accToggler2");
                          }, 
                          function() { 
                              hiddenElements.hide();
                              $(this).text(showCaption);
                              $("#accTogg").removeClass("accToggler2").addClass("accToggler");
                          }
                      )

仍然没有..现在肯定可以工作了,对吧?

              $('.v65-productDisplay').append(
                  $('<tr><td><div class="accToggler" style="font-weight:bold;" id="accTogg">' + showCaption + '</div></td></tr>')
                      .toggle(
                          function() { 
                              hiddenElements.show();
                              $(this).text('Collapse');
                              $("#accTogg").removeClass("accToggler").addClass("accToggler2");
                          }, 
                          function() { 
                              hiddenElements.hide();
                              $(this).text(showCaption);
                              $("#accTogg").removeClass("accToggler2").addClass("accToggler");
                          }
                      )
              );

这就是全部代码,抱歉。

$(this).text('Collapse');

正在替换整个 div,因此它甚至不再存在=/我怎样才能让它只替换 div 内的文本?

.toggle(
  function() { 
      hiddenElements.show();
      $(this).text('Collapse');
      $(".accToggler").removeClass().addClass("accToggler2");
  }, 
  function() { 
      hiddenElements.hide();
      $(this).text(showCaption);
      $(".accToggler2").removeClass().addClass("accToggler");
  }
)

Once the original .accToggler button is clicked the toggle function doesn't seem to be working. The classes are not being added/removed to the button.

I changed it to this:

                      .toggle(
                          function() { 
                              hiddenElements.show();
                              $(this).text('Collapse');
                              $("#accTogg").removeClass("accToggler").addClass("accToggler2");
                          }, 
                          function() { 
                              hiddenElements.hide();
                              $(this).text(showCaption);
                              $("#accTogg").removeClass("accToggler2").addClass("accToggler");
                          }
                      )

And still nothing.. should definitely be working now right?

              $('.v65-productDisplay').append(
                  $('<tr><td><div class="accToggler" style="font-weight:bold;" id="accTogg">' + showCaption + '</div></td></tr>')
                      .toggle(
                          function() { 
                              hiddenElements.show();
                              $(this).text('Collapse');
                              $("#accTogg").removeClass("accToggler").addClass("accToggler2");
                          }, 
                          function() { 
                              hiddenElements.hide();
                              $(this).text(showCaption);
                              $("#accTogg").removeClass("accToggler2").addClass("accToggler");
                          }
                      )
              );

That's the entire code, sorry.

$(this).text('Collapse');

Is replacing the entire div so it doesn't even exist anymore =/ How can I get it to just replace the text inside of the div?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

为你鎻心 2024-12-16 15:15:46

那应该有效。 看看这个 jsFiddle,它是您想要做的事情的一个非常简单的版本。是否抛出任何 JavaScript 错误?

That should work. Take a lookat this jsFiddle which is a very simple version of what you're trying to do. Are there any JavaScript errors being thrown?

楠木可依 2024-12-16 15:15:46
/**
    * Method that checks whether cls is present in element object.
    * @param  {Object} ele DOM element which needs to be checked
    * @param  {Object} cls Classname is tested
    * @return {Boolean} True if cls is present, false otherwise.
*/
function hasClass(ele, cls) {
  return ele.getAttribute('class').indexOf(cls) > -1;
}

/**
    * Method that adds a class to given element.
    * @param  {Object} ele DOM element where class needs to be added
    * @param  {Object} cls Classname which is to be added
    * @return {null} nothing is returned.
*/
function addClass(ele, cls) {
   if (ele.classList) {
     ele.classList.add(cls);
   } else if (!hasClass(ele, cls)) {
    ele.setAttribute('class', ele.getAttribute('class') + ' ' + cls);
  }
}

/**
  * Method that does a check to ensure that class is removed from element.
  * @param  {Object} ele DOM element where class needs to be removed
  * @param  {Object} cls Classname which is to be removed
  * @return {null} Null nothing is returned.
*/
function removeClass(ele, cls) {
  if (ele.classList) {
    ele.classList.remove(cls);
  } else if (hasClass(ele, cls)) {
    ele.setAttribute('class', ele.getAttribute('class').replace(cls, ' '));
 }
}
/**
    * Method that checks whether cls is present in element object.
    * @param  {Object} ele DOM element which needs to be checked
    * @param  {Object} cls Classname is tested
    * @return {Boolean} True if cls is present, false otherwise.
*/
function hasClass(ele, cls) {
  return ele.getAttribute('class').indexOf(cls) > -1;
}

/**
    * Method that adds a class to given element.
    * @param  {Object} ele DOM element where class needs to be added
    * @param  {Object} cls Classname which is to be added
    * @return {null} nothing is returned.
*/
function addClass(ele, cls) {
   if (ele.classList) {
     ele.classList.add(cls);
   } else if (!hasClass(ele, cls)) {
    ele.setAttribute('class', ele.getAttribute('class') + ' ' + cls);
  }
}

/**
  * Method that does a check to ensure that class is removed from element.
  * @param  {Object} ele DOM element where class needs to be removed
  * @param  {Object} cls Classname which is to be removed
  * @return {null} Null nothing is returned.
*/
function removeClass(ele, cls) {
  if (ele.classList) {
    ele.classList.remove(cls);
  } else if (hasClass(ele, cls)) {
    ele.setAttribute('class', ele.getAttribute('class').replace(cls, ' '));
 }
}
给我一枪 2024-12-16 15:15:46

试试这个

$("li").toggle(
  function() { 
      //hiddenElements.show();
      $(this).text("Collapse");
      setTimeout(function () {
        $(".accToggler").removeClass().addClass("accToggler2");
      }, 1);
  }, 
  function() { 
      //hiddenElements.hide();
      $(this).text("NotCollapse");
      setTimeout(function () {
        $(".accToggler2").removeClass().addClass("accToggler");
      }, 1);
  }
)
.accToggler
{
  color:green;
}

.accToggler2
{
  color:red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<ul>
  <li class="accToggler">NotCollapsed</li>
</ul>

try this

$("li").toggle(
  function() { 
      //hiddenElements.show();
      $(this).text("Collapse");
      setTimeout(function () {
        $(".accToggler").removeClass().addClass("accToggler2");
      }, 1);
  }, 
  function() { 
      //hiddenElements.hide();
      $(this).text("NotCollapse");
      setTimeout(function () {
        $(".accToggler2").removeClass().addClass("accToggler");
      }, 1);
  }
)
.accToggler
{
  color:green;
}

.accToggler2
{
  color:red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<ul>
  <li class="accToggler">NotCollapsed</li>
</ul>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文