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

Remove/Delete ":" Colon in an HTML page

本文关键字:删除 冒号 HTML      更新时间:2023-09-26

我无法访问以下 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>并对其做点什么

jQuery 方式,不带regex

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

或者简单地将文本节点更改为空字符串:

$('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 代码已被编辑,所以我在正则表达式中添加了空格,因此它也可以与初始标记一起使用。

尽管目前未经测试,但我建议如下:

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

另一种解决方案。示例: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(':', '');
       });
    })
});