开发者

Remove first line from a Text block if it contains a specific string - using jQuery

I have a table that has some content in it. Not all rows has same content but some of them will have the first sentence as e.g.

Hello, how are you John?

Hello, how are you Mary?

Same pattern with different name.

The row will look like below. And the content in the span comes from database. Yes the text with break tags as well.

<tr style="background-color:#F7F7f7">
<td>
<span id="sp1">Hello How Are you? John <br/><br/>Some text blalablalalbal <br/><br/>"You are Awesome" <br/><br/>----What can I do for you? <br/><br/></span><br />
<span id="sp2">XYZABCD</span><br />
<span id="sp3">12345669060 Some Loren ipsum</span><br />
</td>
</tr>

I want to remove 开发者_运维问答the first sentence from the rows that has Hello, how are you xyzname?.

So I have the following code that works and does what I want:

<script type="text/javascript">
        $(function () {
            $("#btnCleanUp").click(function () {
                $("table tr td").each(function () {
                    var elem = $(this).find('span:contains("Hello How Are you?")');
                    var alltxts = elem.contents().filter(function () {
                        var retm = true;
                        var str = this.nodeValue;
                        if (str != null) {
                            var bstr = str.indexOf("Hello How Are you?");
                            $(this).nodeValue = "";
                            if (bstr >= 0) {
                                retm = false;
                            }
                        }
                        var ret = (this.nodeType == 3) && retm;
                        return ret;
                    });
                    elem.text("");
                    elem.html("<br/>");
                    $(alltxts).each(function () {
                        var hm = $(this);
                        elem.html(elem.html() + hm[0].nodeValue + "<br/><br/>");
                    });
                });
            });
        });
    </script>

Here is the sample demo.

Question:

Can I do this in a better way?

I want to simplify the above code block if possible, I mean logic-wise because I think it can be done in a better way given the potential of jquery.

I am open to do this in C# if that's a better way. Please provide sample code.


$(function () {
        $("#btnCleanUp").click(function () {
            $("table tr td span").each(function () {
                var data = $(this).html();
                data = data.replace(/^Hello how are you\?.*?(<br ?\/?>){1,2}/i,'');
                $(this).html(data);

            });
        });
    });

See here for demo

But i'm not really sure JavaScript is the best place to be doing this.


You can get rid of that $.each loop:

$(function() {
    $("#btnCleanUp").click(function() {
        var elm = $("table tr td").find('span:contains("Hello How Are you?")');
        var alltxts = elm.contents().filter(function() {
            var retm = true;
            var str = this.nodeValue;
            if (str != null) {
                var bstr = str.indexOf("Hello How Are you?");
                $(this).nodeValue = "";
                if (bstr >= 0) {
                    retm = false;
                }
            }
            var ret = (this.nodeType == 3) && retm;
            return ret;
        });
        elm.text("");
        elm.html("<br/>");
        $(alltxts).each(function() {
            var hm = $(this);
            elm.html(elm.html() + hm[0].nodeValue + "<br/><br/>");
        });
    });
});

Fiddle: http://jsfiddle.net/maniator/ZDx4x/12/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜