添加包含动态内容的标签

时间:2013-04-04 13:01:03

标签: jquery tabs href

嗨,当我点击列表的选项时,我想添加具有动态内容的新标签。内容通过给定的href url获得。

<script type="text/javascript" src="http://www.jankoatwarpspeed.com/wp-content/uploads/examples/dynamic_tabs/jquery-1.4.min.js" ></script>
<script type="text/javascript">
    $(document).ready(function() {
        $("#documents a").click(function() {
            addTab($(this));
        });

        $('#tabs a.tab').live('click', function() {
            // Get the tab name
            var contentname = $(this).attr("id") + "_content";

            // hide all other tabs
            $("#content p").hide();
            $("#tabs li").removeClass("current");

            // show current tab
            $("#" + contentname).show();
            $(this).parent().addClass("current");
        });

        $('#tabs a.remove').live('click', function() {
            // Get the tab name
            var tabid = $(this).parent().find(".tab").attr("id");

            // remove tab and related content
            var contentname = tabid + "_content";
            $("#" + contentname).remove();
            $(this).parent().remove();

            // if there is no current tab and if there are still tabs left, show the first one
            if ($("#tabs li.current").length == 0 && $("#tabs li").length > 0) {

                // find the first tab    
                var firsttab = $("#tabs li:first-child");
                firsttab.addClass("current");

                // get its link name and show related content
                var firsttabid = $(firsttab).find("a.tab").attr("id");
                $("#" + firsttabid + "_content").show();
            }
        });
    });
    function addTab(link) {
        // If tab already exist in the list, return
        if ($("#" + $(link).attr("rel")).length != 0)
            return;

        // hide other tabs
        $("#tabs li").removeClass("current");
        $("#content p").hide();

        // add new tab and related content
        $("#tabs").append("<li class='current'><a class='tab' id='" +
            $(link).attr("rel") + "' href='#'>" + $(link).html() + 
            "</a><a href='#' class='remove'>x</a></li>");

        $("#content").append("<p id='" + $(link).attr("rel") + "_content'>" + 
            $(link).attr("href") + "</p>");

        // set the newly added tab as current
        $("#" + $(link).attr("rel") + "_content").show();
    }
</script>

这是我的HTML代码

<div id="main">
<div id="doclist">
    <h2>Documents</h2>
    <ul id="documents">
        <li><a href="#" rel="Document1" title="This is the content of Document1">Document1</a></li>
        <li><a href="http://www.dynamicdrive.com" rel="Document2" title="This is the content of Document2">Document2</a></li>
        <li><a href="http://www.dynamicdrive.com" rel="Document3" title="This is the content of Document3">Document3</a></li>
        <li><a href="http://www.dynamicdrive.com" rel="Document4" title="This is the content of Document4">Document4</a></li>
        <li><a href="http://www.dynamicdrive.com" rel="Document5" title="This is the content of Document5">Document5</a></li>
    </ul>
</div>
<div id="wrapper">
    <ul id="tabs">
        <!-- Tabs go here -->
    </ul>
    <div id="content">
        <!-- Tab content goes here -->
    </div>
</div>
</div>
<div id="Document1" style="display:none;">anvdsanvidsnvsidvnsdovnsdoivnsdovsdovnsovsvsmvpsmvlksdvsmdkvmsdkvmsvsd</div>

它正在运作,但我不想这样。我想要在href中提到的显示内容。 请帮帮我怎样才能做到。

1 个答案:

答案 0 :(得分:1)

它主要取决于您从网址接收的数据类型是什么类型的请求。假设您只想显示HTML页面,可以按如下方式添加iframe。

HTML

<div class="dynamic_content">
    <ul class='url_list'>
        <li><a href='#' data="http://www.example.com">Load Example</a></li>
    </ul>
</div>

的jQuery

$(document).ready(function(){
    $('ul.url_list > li > a').click(function(){
        var me = $(this);
        var url = me.attr('data'); 
        $('.dynamic_content > iframe').remove();
        $('.dynamic_content').append('<iframe src="' + url + '" width="100%" height="300px"></iframe>');
    });
});

另外,请注意我没有测试上面的代码。这是我最重要的事情所以你可能需要测试它。

更新

好的,我已经更新并测试了代码。此外,这是一个working example (JSFiddle)

更新

这是更新的JSFiddle