只替换目标ID's中的字符串
Replace strings in only targeted ID's
我想替换下例中的[space]-[space]部分
This - is - an - example - message
(包括减号前后的空格)
<div class="withthiscss">xx</div>
HTML是:
<p class="product-title" id="fixthistitle">This - is - an - example - message</p>
我只想改变这个ID包含'fixthistitle'。
那么最终结果将是:
<p class="product-title" id="fixthistitle">This<div class="withthiscss">xx</div>is<div class="withthiscss">xx</div>an<div class="withthiscss">xx</div>example<div class="withthiscss">xx</div>message</p>
通常人们只会简单地改变HTML在那里得到你想要的格式,但这是预生成的文本,所以我不能改变这一点。因此,我需要一个JS/jQuery解决方案吗?
也许有人可以分享如何实现这一点?我希望这对某些人有意义。
编辑:对不起,ID必须是唯一的。
不管怎样;谢谢你的回答/解决方案。高度赞赏!
id
应该是唯一的,所以用class
代替
- 您可以使用
html()
来迭代和更新 内容 - 使用
split('-')
拆分内容(或使用regexsplit(/'s+-'s+/)
) - 现在用
join()
连接
<div class="withthiscss">xx</div>
数组$('#fixthistitle').html(function(i, v) {
return v.split('-').join('<div class="withthiscss">xx</div>')
});
.withthiscss {
color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p class="product-title" id="fixthistitle">This - is - an - example - message</p>
使用regex $('#fixthistitle').html(function(i, v) {
return v.split(/'s+-'s+/).join('<div class="withthiscss">xx</div>')
});
.withthiscss {
color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p class="product-title" id="fixthistitle">This - is - an - example - message</p>
你可以在-
上分割文本,然后与div
重新连接:
$('#fixthistitle').html( $('#fixthistitle').html().split(' - ').join('<div class="withthiscss">xx</div>') );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="product-title" id="fixthistitle">This - is - an - example - message</p>
正如在评论中已经提到的,如果您想在多个项目/元素上执行此任务,请使用class
而不是id
。jQuery + regex的方法是:
$('.fixthistitle').each(function(i, v) {
var s = $(this).text();
$(this).html(s.replace(/'s-'s/, '<div class="withthiscss">xx</div>'));
});
一个可能的解决方案:
$('#fixthistitle').html(function(i, html) {
return html.split(/'s+-'s+/).join('<div class="withthiscss">xx</div>');
});
.withthiscss {
color: #00f;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p class="product-title" id="fixthistitle">This - is - an - example - message</p>
$('#fixthistitle').html(function() {
var find = " - ";
var regex = new RegExp(find, "g");
return $(this).html().replace(regex, '<div class="withthiscss">xx</div>');
});
.withthiscss {
color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p class="product-title" id="fixthistitle">This - is - an - example - message</p>
可以使用正则表达式
代替" - "部分"This - is - an - example - message".replace(/ - /g,"<div class='withthiscss'>xx</div>")
现在,由于它在HTML中可用,我们首先需要从HTML标记中获取文本。下面是代码
var originalText = $('#fixthistitle').text(),
transformedText = originalText.replace(/ - /g,"<div class='withthiscss'>xx</div>")
$('#fixthistitle').text(transformedText)
可以这样做:
var k = $("#fixthistitle").text();
str= k.replace(/[ ]+-[ ]+/g,'<div class="withthiscss">xx</div>');
$("#fixthistitle").html(str);
这里的工作代码
id应该是唯一的!!
$("#fixthistitle").each(function() {
var str = $(this).text();
var res = str.replace(" - ", "whatever");
$(this).text(res);
});
但是这将在它找到第一个出现后停止。因此,您需要通过类属性或数据属性来标识所需的行。
或者你可以这样做:
$("p").each(function() {
if ($(this).attr('id') == 'fixthistitle')
{
var str = $(this).text();
var res = str.replace(/'s-'s/g, ' ');
$(this).text(res);
}
});
依赖:jQuery 1.9。但它应该与大多数版本的jQuery兼容
相关文章:
- 字符串替换-不支持格式字符'}'
- Unicode字符串替换不适用于完整的变音符号阿拉伯文本
- Javascript 将字符串替换为
返回为空 - 字符串替换只替换第一个匹配项,可以'不要让它全球化
- javascript字符串替换$1(如果它是某个值)
- 字符串替换正则表达式,开头为空格
- 将 url 中的一个字符串替换为另一个字符串
- 在HTML文本框中查找某些子字符串,然后用其他字符串替换它们
- 我想使用replace函数扫描json对象,然后用字符串替换匹配的单词
- 字符串替换为从右向左的语言(阿拉伯语)
- 将字符串替换为用户键入的文本
- 字符串替换javascript
- JavaScript 将多行字符串替换为多行字符串
- Javascript字符串替换不起作用
- 字符串替换不'当通过android webView JavascriptInterface传递字符串时无法工作
- 我正在尝试用另一个字符串替换一个字符串
- 字符串替换最后一个字符出现的斜杠
- 将具有的字符串替换为$
- 基于查询字符串替换HTML
- Javascript字符串替换在满足长度而不是instantanoeus之前不会生效