如何使用jQuery删除div中的加号(而不是减号)?

如何使用jQuery删除div中的加号(而不是减号)?,jquery,Jquery,我在一个div中得到三种值: 带加号: <div class="thumblock "> +2 <div class="ratingtext "> <div class="raterclear"></div> </div> </h4> <span>votes</span> </div> 这将遍历div,仅用空字符串替换+符号,并将div中的文本设置为新字符串 这将遍历div,仅用空字符串

我在一个div中得到三种值:

带加号:

<div class="thumblock ">
+2
<div class="ratingtext ">
<div class="raterclear"></div>
</div>
</h4>
<span>votes</span>
</div>
这将遍历div,仅用空字符串替换+符号,并将div中的文本设置为新字符串

这将遍历div,仅用空字符串替换+符号,并将div中的文本设置为新字符串。

这应该可以做到

$('.thumblock').html(function(i, oldhtml) {
  return oldhtml.replace('+','');
});
演示


注意:您有一个无效的
h4
结束标记(未打开)


更新

使用这个,因为您有其他插件使用相同的内容

$('.thumblock')
    .contents()
    .filter( function(){
        return this.nodeType == 3;
    }).each(function(){
        this.nodeValue= this.nodeValue.replace(/\+/g,'');
    });
演示在

这应该可以做到

$('.thumblock').html(function(i, oldhtml) {
  return oldhtml.replace('+','');
});
演示


注意:您有一个无效的
h4
结束标记(未打开)


更新

使用这个,因为您有其他插件使用相同的内容

$('.thumblock')
    .contents()
    .filter( function(){
        return this.nodeType == 3;
    }).each(function(){
        this.nodeValue= this.nodeValue.replace(/\+/g,'');
    });
演示在

尝试

$('.thumblock').each(function(){
    var me = $(this);
    $(me).text($(me).text().replace('+',''));
});
试试看

亚历克斯副手

因此,如果你能控制markupp,你应该控制范围内的“操作符” 像这样

<div class="thumblock ">
 <span class="plus">+</span>2
<div class="ratingtext ">

... other

<div class="thumblock ">
 <span class="minus">-</span>2
<div class="ratingtext ">
亚历克斯副手

因此,如果你能控制markupp,你应该控制范围内的“操作符” 像这样

<div class="thumblock ">
 <span class="plus">+</span>2
<div class="ratingtext ">

... other

<div class="thumblock ">
 <span class="minus">-</span>2
<div class="ratingtext ">
在此处测试并解调:

具有不干扰其他
+
符号的优点=将仅替换内容中的前导
+
符号。

在此处测试并解调:

$('.thumblock').text($(this).replace("+",""));
具有不干扰其他
+
符号的优点=将仅替换内容中的前导
+
符号。

尝试:

$('.thumblock').text($(this).replace("+",""));
工作示例:

尝试:

工作示例:


首先,我会重新组织您的html,以便能够访问您所引用的特定值。我会这样做:

<div class="thumblock ">
<span class="thumblockspan">+2</span>
<div class="ratingtext ">
<div class="raterclear"></div>
</div>
</h4>
<span>votes</span>
</div>
var v = $('.thumblockspan').html();
v=v.replace('+','');
$('.thumblockspan').html(v);
或者,用一行:

$('.thumblockspan').html($('.thumblockspan').html().replace('+',''));

正则表达式也是一种选择,但对于这种简单的东西,replace似乎起到了关键作用:)

首先,我会重新组织您的html,这样我就可以访问您所引用的特定值。我会这样做:

<div class="thumblock ">
<span class="thumblockspan">+2</span>
<div class="ratingtext ">
<div class="raterclear"></div>
</div>
</h4>
<span>votes</span>
</div>
var v = $('.thumblockspan').html();
v=v.replace('+','');
$('.thumblockspan').html(v);
或者,用一行:

$('.thumblockspan').html($('.thumblockspan').html().replace('+',''));

正则表达式也是一个选项,但对于这么简单的事情,replace似乎起到了作用:)

@Ryan出于某种原因它不起作用。@Ryan出于某种原因它不起作用。。。但这也将删除
.thumblock
元素中的所有子元素。。。。但这也将删除
.thumblock
元素中的所有子元素。这工作非常完美(我没有粘贴开头的h2标记)。奇怪,由于某种原因,在添加代码后,我不能再投票了(GD星级)。@alexchenco,不太奇怪。。原始代码正在重新编写html,并删除了内部元素上的所有活动事件。。改用更新中的代码..嘿,抱歉,但是如果输出按照上面的编辑更改,我应该如何修改该函数?这工作非常完美(我没有粘贴开头的h2标记)。奇怪,由于某种原因,在添加代码后,我不能再投票了(GD星级)。@alexchenco,不太奇怪。。原始代码正在重新编写html,并删除了内部元素上的所有活动事件。。使用更新中的代码。嘿,抱歉,但是如果输出按照上面的编辑更改,我应该如何修改该函数?非常奇怪,一些答案在这里工作,破坏投票功能(Wordpress的GD star rating插件)。非常奇怪,一些答案在这里工作,破坏投票功能(Wordpress的GD星级插件)。哈,这很奇怪。我刚刚试过,它似乎对我有用。那就用三行版本吧……哈,这很奇怪。我刚刚试过,它似乎对我有用。那就用三行版本吧。。。。
$('.thumblockspan').html($('.thumblockspan').html().replace('+',''));