使用jQuery查找并替换元素的类 [英] Find and replace an element's class using jQuery

查看:95
本文介绍了使用jQuery查找并替换元素的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用的平台不允许我改变已设计的大部分HTML,为了解决这个问题,我需要使用JQuery来查找和替换另一个div的div(或者可能更简单条款(因为我在这里弄错了什么),在HTML中找到文本并用其他文本替换它)。

所以,原文是:

 < div class =title-desc-wrapper has-main-imagedata-content-field =main-image> 

我需要将它替换为:

 < div class =title-desc-wrapper no-main-imagedata-content-field =main-image> 

如果有人对如何做到这一点有所了解,或者可以告诉我如何做,我将非常感激它只需添加类 has-main-image 以及 删除类 no-main-image

  $('。 ')。removeClass(' 标题降序排序wrapper.has-主图像具有-主图像无主图像') ')addClass('; 

..或者您可以使用 .toggleClass() 基本上替换了类:

  $('。title-desc-wrapper.has-main-image')。toggleClass('has-main-image no-main-image'); 


The platform that I'm currently using does not allow me to change much of the HTML that's been designed, so to get around this, I need to use JQuery to find and replace a div with another div (or perhaps in simpler terms (incase I'm getting something wrong here), find text in the HTML and replace it with other text).

So, the original is:

<div class="title-desc-wrapper has-main-image" data-content-field="main-image">

I need it to be replaced with:

<div class="title-desc-wrapper no-main-image" data-content-field="main-image">

If anyone has any insight into how to do this, or could show me how, I would greatly appreciate it.

解决方案

Just add the class has-main-image and remove the class no-main-image:

$('.title-desc-wrapper.has-main-image').removeClass('has-main-image').addClass('no-main-image');

..or you could use .toggleClass() to essentially replace the class:

$('.title-desc-wrapper.has-main-image').toggleClass('has-main-image no-main-image');

这篇关于使用jQuery查找并替换元素的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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