JQuery查找#ID,RemoveClass和AddClass [英] JQuery Find #ID, RemoveClass and AddClass

查看:245
本文介绍了JQuery查找#ID,RemoveClass和AddClass的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下HTML

 < div id =testIDclass =test1> 
< img id =testID2class =test2alt =src =some-image.gif/>
< / div>

我基本上想要#testID2并用.test3类替换.test2类?



我试过了

$ p $ jQuery('#testID2')。find('。 。TEST2 ')replaceWith(' TEST3' );

但是这似乎不起作用?

有什么想法?

解决方案

  jQuery('#testID2')。find  .test2 ')replaceWith(' TEST3' 。); 

语义上,您选择ID testID2 ,那么你正在寻找类 test2 (不存在)的任何后代元素,然后用另一个元素(元素中的任何元素与类 test3 ),也不存在。



您需要这样做:

  jQuery('#testID2')。addClass('test3')。removeClass('test2'); 

选择ID testID2 ,然后将类 test3 添加到它。最后,它从该元素中删除类 test2


I have the following HTML

<div id="testID" class="test1">
        <img id="testID2" class="test2" alt="" src="some-image.gif" />
    </div>

I basically want to get to #testID2 and replace .test2 class with .test3 class ?

I tried

jQuery('#testID2').find('.test2').replaceWith('.test3');

But this doesn't appear to work ?

Any ideas ?

解决方案

jQuery('#testID2').find('.test2').replaceWith('.test3');

Semantically, you are selecting the element with the ID testID2, then you are looking for any descendent elements with the class test2 (does not exist) and then you are replacing that element with another element (elements anywhere in the page with the class test3) that also do not exist.

You need to do this:

jQuery('#testID2').addClass('test3').removeClass('test2');

This selects the element with the ID testID2, then adds the class test3 to it. Last, it removes the class test2 from that element.

这篇关于JQuery查找#ID,RemoveClass和AddClass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆