删除/删除HTML页面中的“:”冒号

时间:2012-05-29 11:30:25

标签: javascript jquery html

我无法访问以下HTML,它使用一些外部JS动态显示。

<td class="quantitybox">
    <span class="qty">Quantity</span>
    <br style="clear:both;">
    :<input type="text" value="1" onkeydown="javascript:QtyEnabledAddToCart();" maxlength="8" size="3" name="QTY.1121309" class="v65-productdetail-cartqty">
</td>

我想要:在使用Jquery删除/删除后,但我没有得到应该使用的处理程序,我应该动态地将一个类应用于<br>并对其做一些事情

4 个答案:

答案 0 :(得分:5)

jQuery方式,没有regex

$('td.quantitybox').contents().filter(function(){       
    return this.nodeType === 3  && // textNode
           $.trim(this.nodeValue) === ":";
}).remove();

或者只是将textnode更改为空字符串:

$('td.quantitybox').contents().filter(function(){       
    return this.nodeType === 3  && // textNode
           $.trim(this.nodeValue) === ":";
})[0].nodeValue = "";

如果您要删除多个带冒号的文本节点 - :

$('td.quantitybox').contents().filter(function() {
    return this.nodeType === 3 && // textNode
    $.trim(this.nodeValue) === ":";
}).each(function() {
    this.nodeValue = "";
});​

如果您想使用regex并且意识到它存在风险:

$('td.quantitybox').html(function(i, old){
    return old.replace(/:\s*</, '<');
});

请注意,问题中的HTML代码已经过编辑,所以我在正则表达式中添加了空格,因此它也适用于初始标记....

答案 1 :(得分:1)

我建议,虽然目前尚未经过测试,但仍有以下内容:

$('td').each(function() {
    var i = this.getElementsByTagName('input'),
        text = i.previousSibling.nodeValue.replace(/:/g, '');
    i.previousSibling.nodeValue = text;
});​

答案 2 :(得分:0)

$(document).ready(function(){
   var texts = $('input').map(function(){
        var text = this.previousSibling.nodeValue;
       this.previousSibling.nodeValue = text.replace(/:/g, '');
   });
});​

答案 3 :(得分:0)

另一种解决方案。示例:http://jsfiddle.net/k25yx/1/

$(document).ready(function() {

    // Cache Object
    var obj  = $('.quantitybox');

    obj.each(function(){
       var that = $(this);

       // remove br          
       that.find('br').remove();

       // trim colon  
       that.html(function(i, val) {
           return val.replace(':', '');
       });
    })
});
相关问题