簡體   English   中英

無法使用JQUERY刪除html內容

[英]Unable to remove html content using JQUERY

我無法刪除ID為"Third" div並在ID為"Fourth" div中顯示結果內容。 請注意:-我需要將#Third內容附加到#Fourth ,然后在以后刪除。


<!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>

您可以使用:

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

工作演示

要么

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

工作演示

 $(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();
        };
    });

試試這個,它將正常工作。

您要將其他元素的“內部html”附加到#Fourth 為了保持標識符完整,您應該這樣做:

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

然后,在稍后階段,您只需刪除#Third

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

您將要初始化此變量

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

但是,當您僅包含內部html時,您會在#Fourth中尋找#Third

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

你應該能夠改變

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

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

真正的問題是,為什么要在第三位到第四位添加然后刪除它? 假設代碼是固定的,則在執行時,您的代碼與此等效。

$("#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.

您可以在http://api.jquery.com/remove/上查看刪除API文檔。

希望這能解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM