使用JQuery替换HTML页面上的特定字符串 [英] Replace a specific string on an HTML page using JQuery

查看:142
本文介绍了使用JQuery替换HTML页面上的特定字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在提供一整段HTML内容,这些内容将被导入并显示在其他人的页面上.页面的所有者在内容Blob周围放置了一个标题,但标题是提供内容的Web服务的名称,而不是我们希望向用户显示的用户友好名称.他们无法弄清楚如何在其末端更改标题,我们不想更改我们的Web服务的名称,并且几天后的演示必须看起来不错.

We are supplying a blob of HTML content to be imported and displayed on someone else's page. The owners of the page put a title around our content blob, but the title is the name of our web service that provides the content, rather than a user-friendly name that we want to display to users. They can't figure out how to change the title on their end, we don't want to change the name of our web service, and it needs to look good for a demo in a few days.

理想的解决方案是让他们弄清楚如何使用其门户工具来定制标题,但这在演示之前不太可能发生.所以我想我们可以在发送它们的内容中包含一些JavaScript,这将更改它们的标题.这将使他们有时间弄清楚他们在做什么.

The ideal solution would be for them to figure out how to use their portal tool to be able to customize titles, but that is not likely to happen before the demo. So I was thinking we could include some JavaScript in the content that we send them, which would change the title for them. This would buy them time to figure out what they are doing.

其标题的代码如下:

<div class="titleClass"> Bad Title </div>

我想将不良标题"替换为良好标题".我想使用jQuery选择器在页面上找到此文本,但不幸的是,有多个div项目具有titleClass类,我只想更改其中之一....是否有我可以使用的选择器检查div标签内的文本"Bad Title"?还是我必须编写一个单独的函数来遍历页面上的所有不同标题,并测试每个函数以查看其是否包含不良标题"?如果我必须执行第二步,是否有人可以共享示例代码?

I would like to replace "Bad Title" with "Good Title". I would like to locate this text on the page using a jQuery selector, but unfortunately there are multiple div items with the class of titleClass and I only want to change one of them.... is there a selector I could use that would also check for the text "Bad Title" inside the div tag? Or do I have to write a separate function that would loop through all the different titles on the page, and test each one to see if it contains "Bad Title"? If I have to do the second, does anyone have sample code to share?

推荐答案

也许您想简化一下?

$(document).ready( function() {
  $('div.titleClass:contains("Bad Title")').text( "Good title" );
});  

查看包含选择器.

这篇关于使用JQuery替换HTML页面上的特定字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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