Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 仅替换目标ID中的字符串_Javascript_Jquery - Fatal编程技术网

Javascript 仅替换目标ID中的字符串

Javascript 仅替换目标ID中的字符串,javascript,jquery,Javascript,Jquery,我想替换下例中的[space]-[space]部分 This - is - an - example - message 包括减号前后的空格 与 HTML是: <p class="product-title" id="fixthistitle">This - is - an - example - message</p> 我只想为包含“fixthistitle”的ID更改此设置 因此,最终结果将是: <p class="product-title" id="f

我想替换下例中的[space]-[space]部分

This - is - an - example - message 
包括减号前后的空格

HTML是:

<p class="product-title" id="fixthistitle">This - is - an - example - message</p>
我只想为包含“fixthistitle”的ID更改此设置

因此,最终结果将是:

<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>
$('.fixthistitle').each(function(i, v) {
  var s = $(this).text();
  $(this).html(s.replace(/\s-\s/, '<div class="withthiscss">xx</div>'));
});
通常情况下,只需简单地更改其中的HTML即可获得所需的格式,但这是预生成的文本,因此我无法更改。因此,我需要一个JS/jQuery解决方案来解决这个问题

也许有人可以分享如何做到这一点?我希望这对某些人来说是有意义的

编辑:

对不起,我的错,一个ID应该是唯一的

无论如何;感谢您提供的众多答案/解决方案。非常感谢

id应该是唯一的,所以请改用class

您可以使用来迭代和更新内容 使用或使用正则表达式拆分内容 现在使用xx加入数组 $'fixthistitle'.htmlfunctioni,v{ 返回v.split'-'。加入'xx' }; .用这个CSS{ 颜色:绿色; }

这是一条示例消息

如注释中所述,如果要对多个项目/元素执行此任务,请使用class而不是id。jQuery+regex方法是:

<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>
$('.fixthistitle').each(function(i, v) {
  var s = $(this).text();
  $(this).html(s.replace(/\s-\s/, '<div class="withthiscss">xx</div>'));
});
$'fixthistitle'.htmlfunction{ var find=-; var regex=新的RegExpfind,g; 返回$this.html.replaceregex,“xx”; }; .用这个CSS{ 颜色:绿色; }

这是一个示例消息

一个可能的解决方案:

$'fixthistitle'.htmlfunctioni,html{ 返回html.split/\s+-\s+/.join'xx'; }; .用这个CSS{ 颜色:00f; }

这是一条示例消息

您可以拆分上面的文本,然后将其与div重新连接:

$'fixthistitle.html$'fixthistitle.html.split'-'。加入'xx';

这是一条示例消息

这可以做到:

var k = $("#fixthistitle").text();
str= k.replace(/[ ]+-[ ]+/g,'<div class="withthiscss">xx</div>');
$("#fixthistitle").html(str);

工作代码

要替换-部分,可以使用正则表达式

"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)

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.x,但它应该适用于大多数版本的jQuery

应该只有一个id作为fixthistitle。如果您想拥有相同的名称,可以改用class,只是在var k=$fixthisttitle.text的末尾遗漏了一点。虽然它可以工作,但可能会导致更大的代码库问题。因此,您可以添加逗号或分号,但在这种情况下,您应该在下一行中添加var关键字