代码之家  ›  专栏  ›  技术社区  ›  Chen Kinnrot

我的代码怎么了[关闭]

  •  0
  • Chen Kinnrot  · 技术社区  · 14 年前

    这是我用javascript写的第一件事,所以我希望它是一个基本错误

    我想达到的目标是: 从一个页面中获取一组链接,在其中加载内容查询链接,并将它们添加到当前页面的列表中。

    追加时出错 在JQ文档中,他们说这个方法可以得到一个JQ对象

    <script type="text/javascript">
        $(document).ready(function () {
            var wtf = $(".download");
            var links = $("a", wtf)
            links.each(function (index) {
                var newFrame = document.createElement("div");
                $(newFrame).load($(this).context.href, function (response, status, xhr) {
                    if (status == "error") {
                        alert("error");
                    }
                    $("a", response).each(function (index) {
                        $("#results").append($(this));
                    });
                });
                //  $("#results").append(newFrame);
            });
        });
    </script>
    
    2 回复  |  直到 14 年前
        1
  •  3
  •   Yi Jiang G-Man    14 年前

    让我们看看我的 猜测 错误检查能力足以满足以下要求:

    // No point really in using context here.
    // This would be better, or at least more readable
    var links = $(".download a");
    
    // No need to add in the argument if you're not actually going to use it
    links.each(function() {
        // Doing this will get you create the jQuery object
        // without having to use the DOM createElement method
        var newFrame = $('<div />');
    
        // this.href is shorter and neater
        newFrame.load(this.href, {
            'html': '.ajax'
        }, function(response, status, xhr) {
            if (status == "error") {
                console.log(xhr);
            }
    
            // This is the odd part - response is a selector?
            // This should loop through each anchor that are a child
            // Of the element selected by the response receieved
            // Using appendTo here is better than looping through
            // each element and using .append() there
            $(response).find("a").appendTo("#results");
    
            // And finally of course because this is .load()
            // Remember that the actual response is also
            // appeneded to the element in question,
            // hence the extra .ajax appearing there
        });
    
        // This occures outside of callback,
        // so it's actually executed *before* the code above
        $("#results").append(newFrame);
    });
    

    关于这段实际工作的代码,请参见jsfiddle: http://jsfiddle.net/E589q/4/

        2
  •  0
  •   Liam Bailey    14 年前

    加载函数没有错误处理功能,它仅用于简单的Ajax功能。

    试试这个

    <script type="text/javascript">
    <!--
     $(document).ready(
    function () {
    
        var links = $("a.download")
        $.each(links,
        function () {
            var url = $(this).attr('href');
            var newFrame = $("<div></div>").appendTo('body').css({position: 'absolute', top: '100px',  left: '100px'});
              $(newFrame).load(url)
                  })
                                })
    
    //-->
    </script>