简体   繁体   中英

Removing elements just won't work with jQuery

I have an autogenerated layout looking like this created by the jQuery.PrettyTextDiff-plugin:

<head>
    <script type="text/jaascript" src="the jquery library"></script>
</head>
<body>
    <div class="diff">
        <span>
            <br/>
        </span>
        Continuing layout...
    </div>
    <script type="text/javascript" src="diff_match_patch.js"></script>
    <script type="text/javascript" src="jquery.pretty-text-diff.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('.diff span:first-child').remove(); // As of my last try
        });
    </script>
</body>

I wish to remove the first span inside the 'diff'-layout in runtime but I can't get it to work. I've tried several solutions found here at SO after a thorough research.

$('.diff').closest('span').remove();
$('.diff span').first().remove(); //recommended solution by the jQuery Foundation - yet it won't work
$('.diff span').remove();
$('.diff').find('span:gt(0)').remove();

Nothing seems to remove the span. How should I do to remove it?

Did you wrapp your jQuery code like:

    $(document).ready(function () {
        $('.diff span').remove();
    });

this should work.

UPDATE: To remove only the first child do:

$('.diff span:first-child').remove();

Fiddle

Try like this…

$('.diff').each(function(){
   $(this).find('span').eq(0).remove();
});

The statements you have provided should work:

$(".diff").remove("span");

fiddle

I believe that the code you're talking about is generated dynamically. You need to wait until is rendered and remove the span tags after it.

You could also fix the problem in the plugin itself, or ask the author of the library to fix the issue for you.

try with this $(".diff").find("span").html("");

Click here .

Somewhere I saw a rapid comment suggesting that I could use CSS to solve this - so I did. It works as a charm.

.diff span {
    display: none;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM