简体   繁体   English

在jQuery加载中使用变量来加载部分html

[英]using variable in jQuery load, to load partial html

I want to load a partial HTML file using AJAX like the below, however the page loads completely and not just the selector. 我想像下面一样使用AJAX加载部分HTML文件,但是页面完全加载,而不仅仅是选择器。 What am I doing wrong? 我究竟做错了什么?

$(function(){
    $('#post-list a').click(function(e){
        var url = $(this).attr('href');
        $('#ajax-div').load(url "#post");
        e.preventDefault();
    });
});

From the JQuery documentation: JQuery文档中:

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选择器。

in you code: 在您的代码中:

$('#ajax-div').load(url "#post");

should be: 应该:

$('#ajax-div').load(url+" #post");

I am assuming the missing + is a typo, but the space between url and #post is important! 我假设缺少的+是一个错字,但是url和#post之间的空格很重要!

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

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