jquery - 如果没有点击,如何组合我的 jQuery 并设置默认内容

标签 jquery

可以用简单的代码组合下面的 jQuery 吗?

$(document).ready(function () {
    $("a.type").live('click', function () {
        var type = $(this).data("id");
        $('#advertisements').load("ads.php?" + type);
    });
    $("a.maincat").live('click', function () {
        var maincat = $(this).data("id");
        $('#advertisements').load("ads.php?" + maincat);
    });
    $("a.subcat").live('click', function () {
        var subcat = $(this).data("id");
        $('#advertisements').load("ads.php?" + subcat);
    });
    $("a.region").live('click', function () {
        var region = $(this).data("id");
        $('#advertisements').load("ads.php?" + region);
    }); 
    $("a.byuser").live('click', function () {
        var byuser = $(this).data("id");
        $('#advertisements').load("ads.php?" + byuser);
    });     
});

根据此代码,如何加载默认内容是通过 $('#advertisements').load("ads.php?"+ 1);

当前需要点击加载特定内容。

让我知道

最佳答案

描述

您可以使用多个选择器,因为您始终需要来自 id 属性的数据 并每次都从同一个脚本加载。

示例

$(document).ready(function () {
    $("a.type,a.maincat,a.subcat,a.region,a.byuser").live('click', function () {
        var id = $(this).data("id");
        $('#advertisements').load("ads.php?" + id);
    });   

    $('#advertisements').load("ads.php?" + 1);
});

更多信息

更新

您确定需要 $(this).data("id") 而不是被点击元素的 ìd 值吗?

如果您想要 id 属性中的值,请执行此操作

var id = $(this).attr("id");

而不是

var id = $(this).data("id");

-jsFiddle Demonstration

关于jquery - 如果没有点击,如何组合我的 jQuery 并设置默认内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8714091/

相关文章:

jquery - 带有页眉和页脚以及动态内容高度的 Css div

javascript - 为什么 Firefox 似乎在 jQuery 对象上有一个 `innerHTML` 属性,但 Chrome 需要 `.get(0)` ?

javascript - Ajax查询json源码并输出结果

javascript - jquery 在 IE 中读取负数时返回 0

javascript - 对 Jquery 和 Css 文件使用 cdn - 覆盖 Prestashop 的默认设置

jquery - 检查 body 负荷(或其他要素)

javascript - 另一个冲突的 jquery 问题

jquery - 使用 jQuery 进行实时搜索时 AJAX 超时

javascript - 调整大小功能不起作用

javascript - 如何提醒 var distance_all (来自 var distance_top + distance_bottom)?