4

I dont have access to the following HTML its displayed dynamically using some External 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>

I want that : after to be Removed/Deleted using Jquery, but i am not getting what handler should be used shall i apply a class to <br> dynamically and do something to it

4

4 回答 4

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

于 2012-05-29T11:34:00.483 回答
1

我建议,虽然目前未经测试,但如下:

$('td').each(function() {
    var i = this.getElementsByTagName('input'),
        text = i.previousSibling.nodeValue.replace(/:/g, '');
    i.previousSibling.nodeValue = text;
});​
于 2012-05-29T11:34:57.063 回答
0
$(document).ready(function(){
   var texts = $('input').map(function(){
        var text = this.previousSibling.nodeValue;
       this.previousSibling.nodeValue = text.replace(/:/g, '');
   });
});​
于 2012-05-29T11:46:05.127 回答
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(':', '');
       });
    })
});
于 2012-05-29T11:51:20.863 回答