简体   繁体   English

jQuery ajax在另一页上加载div的内容不起作用

[英]jQuery ajax load content of a div on another page does not work

Isn't the jQuery's ajax support the loading of a content from another page? jQuery的ajax是否不支持从另一个页面加载内容? I am fine by using the .load , but just curious why? 我可以使用.load很好,但是很好奇为什么?

For example: 例如:

var ajaxModal = $('<div />', {
        'class': 'ajax-modal'
    }),
    $data = $(link).data(),
    options = {
      url: $(link).prop('href') + ' ' + $data.target
    };

ajaxModal.load(options.url, function() {
    ajaxModal.appendTo('body').show(); // works
});

$.ajax(options).done(function (data) {
    ajaxModal.html(data).appendTo('body').show(); // doesn't work
});

From the Docs : 文档中

The .load() method, unlike $.get() , allows us to specify a portion of the remote document to be inserted. $.get()不同, .load()方法允许我们指定要插入的远程文档的一部分。 This is achieved with a special syntax for the url parameter. 这可以通过使用url参数的特殊语法来实现。 If one or more space characters are included in the string, the portion of the string following the first space is assumed to be a jQuery selector that determines the content to be loaded. 如果字符串中包含一个或多个空格字符,则假定字符串中第一个空格之后的部分是确定要加载内容的jQuery选择器。

If you really want to use ajax in your case you can use something like: 如果您确实想在自己的情况下使用ajax,则可以使用类似以下内容的方法:

$.ajax(options).done(function (data) {
    var DivYouWant = $("#DivYouWant", data);
    ajaxModal.html(DivYouWant).appendTo('body').show(); // does work
});

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

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