简体   繁体   English

jQuery Basic插件-从另一个页面加载

[英]jQuery Basic Plugin - Loading from another page

I am very inexperienced with jQuery plugins, and only mildly experienced in jQuery itself. 我对jQuery插件非常缺乏经验,并且对jQuery本身只有一定的经验。

I am trying to create a jQuery plugin that allows me to fill a div with content from another page with GET variables in the URL depending on the parameters that are passed to it. 我正在尝试创建一个jQuery插件,该插件可以使我用另一个页面中的内容填充div ,并根据传递给它的参数在URL中使用GET变量。

Eg $('#mydiv').getstuff(1,2,3) would load it with code from engine.php?a=1&b=2&c=3 . 例如$('#mydiv').getstuff(1,2,3)将使用engine.php?a=1&b=2&c=3代码加载它。

Here is my code so far - I know there are problems with the way it takes in parameters but other than that I can't figure out why it's not working. 到目前为止,这是我的代码-我知道参数的输入方式存在问题,但除此之外,我无法弄清楚为什么它不起作用。

<script type="text/javasctipt" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
    (function($){
        $.fn.extend({ 
            //pass the options variable to the function
            bookmark: function(options) {
                var defaults = {
                    number: 5,
                    order: 1,
                    user: '',
                };

                var options = $.extend(defaults, options);

                return this.each(function() {
                    var o = options;
                    var obj = $(this);
                    var container = $("div", obj);

                    $("div", obj).load("thing.php?number="+o.number+"&order="+o.order+"&user="+o.user);
                });
            }
        });
    })(jQuery);

    $(document).ready(function() {
        $('#thing').bookmark(1,2,3);
    });
</script>

<div id="thing"></div>

The problem I see is in these lines: 我看到的问题在于以下几行:

var obj = $(this);
$("div", obj).load("thing.php?number="+o.number+"&order="+o.order+"&user="+o.user);

That last line says, "Get all div tags within obj," which is to say, all div tags within the current jQuery context, which is div#thing. 最后一行说:“在obj中获取所有div标签”,也就是说,当前jQuery上下文中的所有div标签为div#thing。 Just change it to: 只需将其更改为:

$(this).load("thing.php?number="+o.number+"&order="+o.order+"&user="+o.user);

It seems almost right. 看来差不多了。

Take a look at the syntax here . 这里看看语法

$("div", obj) ? $("div", obj)吗? What is obj for? obj的作用是什么? Also, this is probably just an example, but you'll need #div . 另外,这可能只是一个示例,但是您需要#div Also, remember the #div has to exist, or the call won't even be executed. 另外,请记住#div必须存在,否则将无法执行调用。

Also, take a look at the javascript error console, maybe that gives you any hints to why it is not working? 另外,看看javascript错误控制台,也许可以为您提供任何提示,为什么它不起作用? Any error perhaps? 可能有任何错误吗?

This line - 这条线-

$("div", obj).load("thing.php?number="+o.number+"&order="+o.order+"&user="+o.user);

is the equivalent of - 等于-

$(obj).find("div").load("thing.php?number="+o.number+"&order="+o.order+"&user="+o.user);

So you're searching for a div inside obj which, in your example is a div with nothing inside it. 因此,您要在obj搜索div,在您的示例中,该div是其中没有任何内容的div。 So that code will return no elements and your load function won't work. 因此该代码将不返回任何元素,并且您的load功能将无法正常工作。 You could load straight into $(obj) or write some code that checked what element(s) your plugin was been called on before deciding where to inject the load content. 您可以直接load$(obj)或编写一些代码,以在决定将load内容注入何处之前检查调用了您的插件的哪些元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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