简体   繁体   中英

Unable to remove html content using JQUERY

I am unable to remove the div with ID "Third" and display the result content in div with ID "Fourth" . Please Note :- I need to append #Third content to #Fourth and then remove at later stage.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="../jquery-ui-1.10.4.custom/js/jquery-1.10.2.js" type="text/javascript"></script>
    <script src="../jquery-ui-1.10.4.custom/js/jquery-ui-1.10.4.custom.min.js" type="text/javascript"></script>
    <link href="../jquery-ui-1.10.4.custom/css/ui-lightness/jquery-ui-1.10.4.custom.min.css"
        rel="stylesheet" type="text/css" />

    <script type="text/javascript">
        $(function() {
            var widget = {}
            $("#Vshowdialog").click(function() {
                widget.doInitialize();
            });
            widget.doInitialize = function() {
                $("#Fourth").append($("#First").html());
                $("#Fourth").append($("#Second").html());
                $("#Fourth").append($("#Third").html());
                widget.showLightBox();
            };
            widget.showLightBox = function() {
            text = $('#Fourth').html();
            $(text).find("#Third").remove();
            $("#Fourth").dialog();
            };
        });
    </script>

</head>
<body>
    <input type="button" id="Vshowdialog" value="ShowDialog" />
    <div id="First">
        <div id="content">
            This is my main Firstcontent.</div>
        <div id="sidebar">
            This is a FirstSideBar</div>
    </div>
    <div id="Second">
        <div id="secondOne">
            This is my main Second content.</div>
        <div id="secondTwo">
            This is a second sidebar</div>
    </div>
    <div id="Third">
    hello
        <div id="thirdOne">
            This is my main third content.</div>
        <div id="thirdTwo">
            This is a third sidebar</div>
    </div>
    <div id="Fourth">
    </div>
</body>
</html>

You can use:

$('#Fourth').append($('#third'));
$('#thirdOne').unwrap();

Working Demo

or

$('#Fourth').append($('#Third')).find('#thirdOne').unwrap();

Working Demo

 $(function() {
        var widget = {}
        $("#Vshowdialog").click(function() {
            widget.doInitialize();
        });
        widget.doInitialize = function() {
            $("#Fourth").append($("#First").html());
            $("#Fourth").append($("#Second").html());
            $("#Fourth").append($("#Third").html());
            widget.showLightBox();
        };
        widget.showLightBox = function() {
        $('#Fourth').find("#thirdOne").remove();
        $('#Fourth').find("#thirdTwo").remove();
        $("#Fourth").dialog();
        };
    });

Try this it will work fine.

You're appending the "inner html" of the other elements to #Fourth ; to keep the identifiers intact you should do this:

$('#First,#Second,#Third').appendTo('#Fourth');

Then, at a later stage you can simply remove #Third :

$('#Third').remove();

you'll want to initialize this variable

text = $('#Fourth').html();

But your looking for #Third within #Fourth when you only included the inner html, specificly

var text = $('#Fourth').html(); //contents of fourth
$(text).find("#Third").remove(); //third was added as inner Html without the wrapping container.

You should be able to just change

 $("#Fourth").append($("#Third").html());

to

 $("#Fourth").append($("#Third"));

The real question is why are you adding Third to Fourth and then removing it? Your code is equivalent to this, in execution, assuming it was fixed.

$("#Fourth").append($("#Third"));
$("#Fourth").find("#Third").remove();
//cache the third div content
var tempThird = $('#Third');
//add your result to Fourth div
$('#Fourth').html("resultcontent");
//remove the third div
$('#Third').remove();
//tempThird will still have the essence of #Third div which you can use anywhere.

You can check the remove API documentation on http://api.jquery.com/remove/

Hope this solves your problem.

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